Class MetaAppWebhooks

java.lang.Object
com.bytedesk.social.meta.MetaAppWebhooks

@RestController @RequestMapping("/meta/webhooks") public class MetaAppWebhooks extends Object
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
  • Field Details

  • 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

      private void setDefaultUser(String userId)
      设置默认用户
    • 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

      private String bytesToHex(byte[] bytes)
      将字节数组转换为十六进制字符串
      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页面ID
      pageAccessToken - 页面访问令牌
      workgroupUid - 工作组ID
      orgUid - 组织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 - 工作组ID
      orgUid - 组织ID
    • determineMessageType

      private String determineMessageType(com.fasterxml.jackson.databind.JsonNode message)
      确定消息类型
      Parameters:
      message - Meta消息对象
      Returns:
      消息类型字符串
    • createAndSendTextMessage

      private void createAndSendTextMessage(String content, String senderId, VisitorResponse visitor, ThreadProtobuf thread, String workgroupUid, String orgUid)
      创建并发送文本消息到客服系统
      Parameters:
      content - 消息内容
      senderId - 发送者ID
      thread - 会话信息
      workgroupUid - 工作组ID
      orgUid - 组织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页面ID
      pageAccessToken - 页面访问令牌
      workgroupUid - 工作组ID
      orgUid - 组织ID
      Returns:
      访客请求对象