[仅需1步]企业微信群机器人[0基础接入][java]
- 背景
- 介绍
- 使用
- 测试
- 项目
背景
公司需要把日常的服务器错误抛到企业微信群中,我正好记录下使用企业微信群机器人…
介绍
企业微信群机器人
应用介绍
企业微信是腾讯微信团队打造的企业通讯与办公工具,具有与微信一致的沟通体验,丰富的OA应用,和连接微信生态的能力,可帮助企业连接内部、连接生态伙伴、连接消费者。
企业微信群机器人是企业微信的内置功能,可在企微群内推送消息、提醒群成员等。企业微信机器人支持webhook协议的自定义接入,来实现信息的自动同步。
企业微信官网:https://work.weixin.qq.com/
(opens new window)
应用场景介绍
通过腾讯云HiFlow场景连接器,可以零代码的设置企业微信机器人自动/定时发消息的规则,比如:每周五下午6点定时发送周报;收到新的销售线索/报表自动发送消息通知管理群等。
应用支持的触发条件和执行操作
企业微信群机器人支持的执行操作如下:
发送群图文消息:发送图片+文本标题的消息样式到企业微信群,支持给图片设置跳转链接
发送富文本消息:发送富文本消息到企业微信群,可以给文本添加样式,比如加粗、更改颜色、加超链接等
发送文本消息:发送纯文本消息到企业微信群,并且可以@提醒指定用户
应用账号和参数配置(或其他相关)
(一)使用企业微信群机器人应用的前置条件
需要有企业微信并已加入企业
需要有企业微信群管理员权限
(二)如何在企业微信中添加群机器人
如果没有可用的企业微信群机器人,请先在企业微信中添加新的群机器人。
在目标群聊右上角点击“..."后选择【添加群机器人】
选择【新创建一个机器人】,输入群机器人名称后点击【添加机器人】
添加成功后即可获得webhook地址,点击【复制地址】后粘贴至相应位置
(三)添加新的机器人账户
添加新的机器人账户需要:
企业微信群机器人名称:可自定义填写,建议与对应的企微群机器人同名
企业微信群机器人webhook地址:需要把获取到的webhook地址添加到HiFlow企业微信群机器人账户设置中如果目标群里您已添加群机器人,您可以右键单击群机器人【查看详情】后获取群机器人webhook地址如果目标群里您没有添加群机器人,请按照教程第二步在目标群中添加群机器人,然后获取到此机器人的webhook地址
使用
WxProducerController.java
package com.dongtech.mywxwork_bot.wx;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;@Controller
public class WxProducerController {private static final Logger logger = LoggerFactory.getLogger(WxProducerController.class);final String APIWXBOT = "/WXBOT";final String LOGTITLE = "[dongtech-server] ["+APIWXBOT+"]";@Autowiredprivate WxProducerService wxProducerService;@Autowiredprivate WxPushService wxPushService;/*** 发送企业微信机器人消息* @param request* @throws IOException*/@RequestMapping(value=APIWXBOT)@ResponseBodypublic void sendWXBOT(HttpServletRequest request) throws IOException {String message = request.getParameter("message");if (message == null || message == "") {return;}String msg = GetCurrentTime()+message;String logMsg = LOGTITLE+" WXBOT Receive Message: "+msg;logger.info(logMsg);WxRequest wxRequest = new WxRequest();wxRequest.message = msg;wxPushService.publishEvent(wxRequest);}private String GetCurrentTime(){LocalDateTime localDateTime = LocalDateTime.now();System.out.println(localDateTime);SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss||");return format.format(new Date());}
}
WxProducerService.java
package com.dongtech.mywxwork_bot.wx;import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;@Service
public class WxProducerService {private static final Logger logger = LoggerFactory.getLogger(WxProducerService.class);final String APIWXBOT = "/WXBOT";final String LOGTITLE = "[dongtech-server] ["+APIWXBOT+"]";@Autowiredprivate WxPushService wxPushService;@Value("${wx.wxbot.token}")public String WXBOTTOKEN;private Queue<WxRequest> wxRequestQueue = new LinkedList<WxRequest>();public void sendMessage(WxRequest wxRequest) throws IOException {if (wxRequest.message == null || wxRequest.message == "") {return;}HttpClient httpclient = HttpClients.createDefault();HttpPost httppost = new HttpPost(WXBOTTOKEN);httppost.addHeader("Content-Type", "application/json; charset=utf-8");//构建一个json格式字符串textMsg,其内容是接收方需要的参数和消息内容String textMsg = "{\"msgtype\":\"text\",\"text\":{\"content\":\"SERVER:"+ wxRequest.message+"\"},\"isAtAll\":false}}";StringEntity se = new StringEntity(textMsg, "utf-8");httppost.setEntity(se);HttpResponse response = httpclient.execute(httppost);if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {String msg = LOGTITLE+" WXBOT Send Success : "+wxRequest.message;logger.info(msg);}else{wxPushService.publishEvent(wxRequest);}}public void Receive(WxRequest wxRequest){wxRequestQueue.offer(wxRequest);}public void DoSend() throws IOException {WxRequest req = wxRequestQueue.poll();if (req == null){return;}send(req);}/*** 发送* @param wxRequest*/public void send(WxRequest wxRequest) throws IOException {sendMessage(wxRequest);}
}
测试
http://localhost:10010/WXBOT?message=我是一个机器人
项目
github