微信自动聊天机器人基础框架
立即下载
资源介绍:
聊天机器人基础框架
package com.wechat.bot.controller;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wechat.bot.handler.WeChatMessageHandler;
@Slf4j
@RestController
@RequestMapping("/wechat")
public class WeChatController {
@Autowired
private WxMpService wxMpService;
@Autowired
private WeChatMessageHandler messageHandler;
@GetMapping
public String checkSignature(@RequestParam String signature,
@RequestParam String timestamp,
@RequestParam String nonce,
@RequestParam String echostr) {
if (wxMpService.checkSignature(timestamp, nonce, signature)) {
return echostr;
}
return "非法请求";
}
@PostMapping
public String handleMessage(@RequestBody String requestBody,
@RequestParam String signature,
@RequestParam String timestamp,
@RequestParam String nonce) {
try {
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
return "非法请求";
}
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
WxMpXmlOutMessage outMessage = messageHandler.handle(inMessage, null, wxMpService, null);
return outMessage.toXml();
} catch (Exception e) {
log.error("处理微信消息出错", e);
return "";
}
}
}