老鬼的博客 来都来啦,那就随便看看吧~
微信公众号如何实现微信聊天机器人
发布于: 2021-03-27 更新于: 2021-03-29 分类于:  阅读次数: 

一:介绍

1
2
3
4
5
6
如何实现微信聊天,首先你要启用服务器配置,然后后台配置一个
客服,然后再找一个开源的聊天对话的三方接口,主要步骤如下:
1.服务器配置
2.添加客服
3.对接三方接口
4.通过微信客户接口回复消息
  • 机器人

青云客网络-人工智能聊天接口
图灵机器人
微信-智能对话

  • 消息

微信接收普通消息接口
客服接口

1
2
3
4
5
6
7
8
9
10
1.发送文本带换行符
- 使用\r\n记录
2.发送文本带超链接
{
"touser":"oH3R1wuKFGosqUsEJV8wg_1hHLNI",
"text":{
"content":"<a href="https://jayden.matchcess.com">老鬼的博客</a>"
},
"msgtype":"text"
}

二:上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.tohours.wechatapi.controller.api.msg;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.tohours.wechatapi.controller.BaseController;
import com.tohours.wechatapi.entity.WxMsg;
import com.tohours.wechatapi.utils.HttpUtils;
import com.tohours.wechatapi.utils.TohoursUtils;
import com.tohours.wechatapi.utils.WechatapiUtils;
import com.tohours.wechatapi.utils.WxUtils;

import lombok.extern.slf4j.Slf4j;

/**
* @desc 接收事件推送
* @author RenJie
* @date 2021-03-25
*
*/
@RestController
@RequestMapping("/api/msg/event")
@Slf4j
@SuppressWarnings("rawtypes")
@CrossOrigin
public class EventController extends BaseController {

/**
* @desc 服务器server
* @return
* @throws Exception
*/
@RequestMapping(value = "/accept")
public String accept(String signature, String timestamp, String nonce, String echostr,
HttpServletRequest request) throws Exception {
log.info("signature:" + signature);
log.info("timestamp:" + timestamp);
log.info("nonce:" + nonce);
log.info("echostr:" + echostr);
boolean flag = WxUtils.CheckSignature(signature, timestamp, nonce);
log.info("flag:" + flag);
String data = TohoursUtils.inputStream2String(request.getInputStream(), "UTF-8");
log.info("data:" + data);

if(StringUtils.isNotEmpty(data)) {
//记录推送消息
JSONObject json = WechatapiUtils.xml2Json(data).getJSONObject("xml");
log.info("json:" + json.toString());
WxMsg wm = new WxMsg();
wm.setXml(data);
wm.setJson(json.toString());
wm.setFromUserName(json.containsKey("FromUserName") ? json.getString("FromUserName") : null);
wm.setToUserName(json.containsKey("ToUserName") ? json.getString("ToUserName") : null);
wm.setMsgType(json.containsKey("MsgType") ? json.getString("MsgType") : null);
wm.setContent(json.containsKey("Content") ? json.getString("Content") : null);
wm.setMsgTypeDesc(loadMsgTypeDesc(wm.getMsgType()));
wm.setMsgId(json.containsKey("MsgId") ? json.getString("MsgId") : null);
wm.setEvent(json.containsKey("Event") ? json.getString("Event") : null);
wm.setTicket(json.containsKey("Ticket") ? json.getString("Ticket") : null);
wm.setEventKey(json.containsKey("EventKey") ? json.getString("EventKey") : null);
wm.setLatitude(json.containsKey("Latitude") ? json.getString("Latitude") : null);
wm.setLongitude(json.containsKey("Longitude") ? json.getString("Longitude") : null);
wm.setPrecision(json.containsKey("Precision") ? json.getString("Precision") : null);
wm.setEventDesc(loadEventDesc(wm.getEvent(),wm.getEventKey(),wm.getTicket(),wm.getLatitude()));
wm.setInsertTime(new Date());

super.wxMsgService.save(wm);

//对接聊天
if("text".equalsIgnoreCase(wm.getMsgType())) {
String result = HttpUtils.httpGet(String.format("https://api.qingyunke.com/api.php?key=free&appid=0&msg=%s", WechatapiUtils.urlEncode(wm.getContent())));
log.info("result:" + result);
JSONObject r = JSONObject.parseObject(result);
String content = r.getString("content");
JSONObject rv = wxMsgService.kefuSendText(wm.getFromUserName(), content);
log.info("rv:" + rv.toString());
}else if(!"event".equalsIgnoreCase(wm.getMsgType())){
String content = "臣妾只能识别文字!!!";
JSONObject rv = wxMsgService.kefuSendText(wm.getFromUserName(), content);
log.info("rv:" + rv.toString());
}
}


if(flag) {
return echostr;
}else {
return null;
}
}

/**
* @desc 根据事件类型加载事件描述
* @param event
* @return
*/
private String loadEventDesc(String event,String eventKey,String ticket,String latitude) {
if(StringUtils.isNotEmpty(event)) {
if(StringUtils.isEmpty(eventKey)) {
if("subscribe".equalsIgnoreCase(event)) {
return "关注/取消关注事件-订阅";
}else if("unsubscribe".equalsIgnoreCase(event)) {
return "关注/取消关注事件-取消订阅";
}
}else {
if(StringUtils.isNotEmpty(ticket)) {
if("subscribe".equalsIgnoreCase(event)) {
return "扫描带参数二维码事件-未关注时扫描";
}else if("SCAN".equalsIgnoreCase(event)) {
return "扫描带参数二维码事件-已关注时扫描";
}

}else if("LOCATION".equalsIgnoreCase(event)) {
return "上报地理位置事件";
}else if("CLICK".equalsIgnoreCase(event)) {
return "自定义菜单事件";
}
}

}
return null;
}

/**
* @desc 根据消息类型加载消息描述
* @param msgType
* @return
*/
private String loadMsgTypeDesc(String msgType) {
if(StringUtils.isNotEmpty(msgType)) {
if("text".equalsIgnoreCase(msgType)) {
return "文本消息";
}else if("image".equalsIgnoreCase(msgType)) {
return "图片消息";
}else if("voice".equalsIgnoreCase(msgType)) {
return "语音消息";
}else if("video".equalsIgnoreCase(msgType)) {
return "视频消息";
}else if("shortvideo".equalsIgnoreCase(msgType)) {
return "小视频消息";
}else if("location".equalsIgnoreCase(msgType)) {
return "地理位置消息";
}else if("link".equalsIgnoreCase(msgType)) {
return "链接消息";
}
}
return null;
}

public static void main(String[] args) {
String xml = "<xml><ToUserName><![CDATA[gh_37d5b9ea1845]]></ToUserName>\r\n" +
"<FromUserName><![CDATA[oH3R1wuKFGosqUsEJV8wg_1hHLNI]]></FromUserName>\r\n" +
"<CreateTime>1616735646</CreateTime>\r\n" +
"<MsgType><![CDATA[text]]></MsgType>\r\n" +
"<Content><![CDATA[123]]></Content>\r\n" +
"<MsgId>23146089719737789</MsgId>\r\n" +
"</xml>\r\n" +
"";
JSONObject json = WechatapiUtils.xml2Json(xml);
System.out.println(json.toString());
}
}
*************感谢您的阅读*************