Package com.bytedesk.social.meta
Class MetaAppWebhooks
java.lang.Object
com.bytedesk.social.meta.MetaAppWebhooks
Meta平台的Webhooks处理类
处理来自Meta平台的webhook事件
https://developers.facebook.com/docs/messenger-platform/webhooks
测试:
1. ngrok http 9003,生成url
2. 在微语管理后台-》客服-》渠道-》Meta-》添加Messenger应用
3. 获取到第2步生成的webhook url,使用上面第1步生成的url替换掉前面域名
如:https://338357194751.ngrok-free.app/meta/webhooks/1689753892159616
4. 填写到Meta平台的webhook url中,验证口令使用在第3步中生成的验证token
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classGraph API交互类class消息接收处理类static class内部用户类 -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final IMessageSendServiceprivate final MetaAppRestServiceprivate final MetaMessageDemoprivate final MetaMessageServiceprivate final com.fasterxml.jackson.databind.ObjectMapperprivate final UidUtilsprivate Map<String,MetaAppWebhooks.User> private final VisitorRestService -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate VisitorRequestbuildVisitorRequest(MetaAppWebhooks.User user, com.fasterxml.jackson.databind.JsonNode webhookEvent, String text, String pageId, String pageAccessToken, String workgroupUid, String orgUid) 根据Meta用户信息构建访客请求对象 参考WeChatWorkMessageApiService.buildVisitorRequest的实现模式private StringbytesToHex(byte[] bytes) 将字节数组转换为十六进制字符串private voidcreateAndSendTextMessage(String content, String senderId, VisitorResponse visitor, ThreadProtobuf thread, String workgroupUid, String orgUid) 创建并发送文本消息到客服系统private StringdetermineMessageType(com.fasterxml.jackson.databind.JsonNode message) 确定消息类型private voidhandleMessagingEvent(com.fasterxml.jackson.databind.JsonNode webhookEvent) 处理messaging事件private voidhandlePageChanges(com.fasterxml.jackson.databind.JsonNode entry) 处理页面变更事件private voidhandleSenderPsid(String senderPsid, com.fasterxml.jackson.databind.JsonNode webhookEvent, boolean guestUser) 处理发送者PSIDprivate booleanisGuestUser(com.fasterxml.jackson.databind.JsonNode webhookEvent) 检查是否为访客用户private voidprocessVisitorMessage(MetaAppWebhooks.User user, com.fasterxml.jackson.databind.JsonNode webhookEvent, String text, String pageId, String pageAccessToken, String workgroupUid, String orgUid) 处理访客消息,对接到微语系统中 参考WeChatWorkMessageApiService.processVisitorMessage的实现模式private ObjectreceiveAndReturn(MetaAppWebhooks.User user, com.fasterxml.jackson.databind.JsonNode webhookEvent, boolean isUserRef) 接收并返回处理结果org.springframework.http.ResponseEntity<?>receiveWebhook(String uid, String rawPayload, jakarta.servlet.http.HttpServletRequest request) 处理webhook的POST请求 用于接收来自Meta平台的事件通知private voidsendToCustomerService(com.fasterxml.jackson.databind.JsonNode message, VisitorResponse visitor, ThreadProtobuf thread, String workgroupUid, String orgUid) 将Meta访客消息发送给客服 统一使用MetaMessageService处理消息,参考WeChatWorkMessageApiService.sendToCustomerService的实现模式private voidsetDefaultUser(String userId) 设置默认用户private booleanverifyRequestSignature(String appSecret, jakarta.servlet.http.HttpServletRequest request, String payload) 验证请求签名 验证回调是否来自Facebookorg.springframework.http.ResponseEntity<?>verifyWebhook(String uid, String mode, String token, String challenge) 处理webhook的GET请求 用于处理Meta平台的webhook验证 GET "/meta/webhooks/1666799623995648?
-
Field Details
-
metaAppRestService
-
metaMessageService
-
objectMapper
private final com.fasterxml.jackson.databind.ObjectMapper objectMapper -
visitorRestService
-
messageSendService
-
uidUtils
-
users
-
metaMessageDemo
-
-
Constructor Details
-
MetaAppWebhooks
public MetaAppWebhooks()
-
-
Method Details
-
verifyWebhook
@GetMapping("/{uid}") public org.springframework.http.ResponseEntity<?> verifyWebhook(@PathVariable String uid, @RequestParam(value="hub.mode",required=false) String mode, @RequestParam(value="hub.verify_token",required=false) String token, @RequestParam(value="hub.challenge",required=false) String challenge) 处理webhook的GET请求 用于处理Meta平台的webhook验证 GET "/meta/webhooks/1666799623995648? hub.mode=subscribe &hub.challenge=475852048 &hub.verify_token=pdzcuJ947hzpwWi9IgYEbuFPNxNH0O8L", parameters={masked} -
receiveWebhook
@PostMapping("/{uid}") public org.springframework.http.ResponseEntity<?> receiveWebhook(@PathVariable String uid, @RequestBody String rawPayload, jakarta.servlet.http.HttpServletRequest request) 处理webhook的POST请求 用于接收来自Meta平台的事件通知 -
verifyRequestSignature
private boolean verifyRequestSignature(String appSecret, jakarta.servlet.http.HttpServletRequest request, String payload) 验证请求签名 验证回调是否来自Facebook- Parameters:
request- HTTP请求payload- 请求体- Returns:
- 验证是否通过
-
handlePageChanges
private void handlePageChanges(com.fasterxml.jackson.databind.JsonNode entry) 处理页面变更事件 -
handleMessagingEvent
private void handleMessagingEvent(com.fasterxml.jackson.databind.JsonNode webhookEvent) 处理messaging事件 -
handleSenderPsid
private void handleSenderPsid(String senderPsid, com.fasterxml.jackson.databind.JsonNode webhookEvent, boolean guestUser) 处理发送者PSID -
setDefaultUser
设置默认用户 -
receiveAndReturn
private Object receiveAndReturn(MetaAppWebhooks.User user, com.fasterxml.jackson.databind.JsonNode webhookEvent, boolean isUserRef) 接收并返回处理结果 -
isGuestUser
private boolean isGuestUser(com.fasterxml.jackson.databind.JsonNode webhookEvent) 检查是否为访客用户 -
bytesToHex
将字节数组转换为十六进制字符串- Parameters:
bytes- 字节数组- Returns:
- 十六进制字符串
-
processVisitorMessage
private void processVisitorMessage(MetaAppWebhooks.User user, com.fasterxml.jackson.databind.JsonNode webhookEvent, String text, String pageId, String pageAccessToken, String workgroupUid, String orgUid) 处理访客消息,对接到微语系统中 参考WeChatWorkMessageApiService.processVisitorMessage的实现模式- Parameters:
user- 用户信息webhookEvent- webhook事件text- 文本消息内容pageId- Meta页面IDpageAccessToken- 页面访问令牌workgroupUid- 工作组IDorgUid- 组织ID
-
sendToCustomerService
private void sendToCustomerService(com.fasterxml.jackson.databind.JsonNode message, VisitorResponse visitor, ThreadProtobuf thread, String workgroupUid, String orgUid) 将Meta访客消息发送给客服 统一使用MetaMessageService处理消息,参考WeChatWorkMessageApiService.sendToCustomerService的实现模式- Parameters:
message- Meta消息对象(JsonNode格式)visitor- 访客信息thread- 会话信息workgroupUid- 工作组IDorgUid- 组织ID
-
determineMessageType
确定消息类型- Parameters:
message- Meta消息对象- Returns:
- 消息类型字符串
-
createAndSendTextMessage
private void createAndSendTextMessage(String content, String senderId, VisitorResponse visitor, ThreadProtobuf thread, String workgroupUid, String orgUid) 创建并发送文本消息到客服系统- Parameters:
content- 消息内容senderId- 发送者IDthread- 会话信息workgroupUid- 工作组IDorgUid- 组织ID
-
buildVisitorRequest
private VisitorRequest buildVisitorRequest(MetaAppWebhooks.User user, com.fasterxml.jackson.databind.JsonNode webhookEvent, String text, String pageId, String pageAccessToken, String workgroupUid, String orgUid) 根据Meta用户信息构建访客请求对象 参考WeChatWorkMessageApiService.buildVisitorRequest的实现模式- Parameters:
user- Meta用户webhookEvent- webhook事件text- 文本消息pageId- Meta页面IDpageAccessToken- 页面访问令牌workgroupUid- 工作组IDorgUid- 组织ID- Returns:
- 访客请求对象
-