搜索
热搜: 活动 交友 discuz
Hi~登录注册
查看: 2436|回复: 0

FB小游戏开发准备五 (Java 创建 Webhook 服务 提供Messenger 智能助手体验)

[复制链接]

6

主题

10

帖子

149

积分

注册会员

Rank: 2

积分
149
发表于 2018-10-9 09:55:21 | 显示全部楼层 |阅读模式
本帖最后由 破晓 于 2018-10-10 10:28 编辑

Messenger 智能助手 官方文档


https://developers.facebook.com/ ... arted/webhook-setup
https://developers.facebook.com/docs/messenger-platform/webhook


官方文档服务搭建是Node.js 的示例


这里说说 如何创建Java 示例


首先创建Servlet: webhook
WebhookServlet.java
  1. package com.airmyth.servlet;

  2. import java.io.ByteArrayOutputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.PrintWriter;

  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletInputStream;
  8. import javax.servlet.annotation.WebServlet;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;

  12. import com.airmyth.util.JsonUtil;

  13. @WebServlet("/webhook")
  14. public class WebhookServlet extends HttpServlet {

  15.         /**
  16.          * Constructor of the object.
  17.          */
  18.         public WebhookServlet() {
  19.                 super();
  20.         }

  21.         /**
  22.          * Destruction of the servlet. <br>
  23.          */
  24.         public void destroy() {
  25.                 super.destroy(); // Just puts "destroy" string in log
  26.                 // Put your code here
  27.         }

  28.         // 使用Messenger后台生成的token
  29.         private String faceToken = "**************tn3ADVR5nFEZA036wnFY94lt*************";
  30.         /**
  31.          * The doGet method of the servlet. <br>
  32.          *
  33.          * This method is called when a form has its tag value method equals to get.
  34.          *
  35.          * @param request the request send by the client to the server
  36.          * @param response the response send by the server to the client
  37.          * @throws ServletException if an error occurred
  38.          * @throws IOException if an error occurred
  39.          */
  40.         public void doGet(HttpServletRequest request, HttpServletResponse response)
  41.                         throws ServletException, IOException {

  42.                 doPost(request, response);
  43.         }

  44.         /**
  45.          * The doPost method of the servlet. <br>
  46.          *
  47.          * This method is called when a form has its tag value method equals to post.
  48.          *
  49.          * @param request the request send by the client to the server
  50.          * @param response the response send by the server to the client
  51.          * @throws ServletException if an error occurred
  52.          * @throws IOException if an error occurred
  53.          */
  54.         public void doPost(HttpServletRequest request, HttpServletResponse response)
  55.                         throws ServletException, IOException {

  56.                 response.setContentType("text/html");
  57.                
  58.                 ServletInputStream sis = request.getInputStream();

  59.         ByteArrayOutputStream bos = new ByteArrayOutputStream();
  60.         DataOutputStream dos = new DataOutputStream(bos);
  61.         int bytes = 0;
  62.         byte[] buffer = new byte[8192];
  63.         while ((bytes = sis.read(buffer, 0, 8192)) != -1)
  64.         {
  65.             dos.write(buffer, 0, bytes);
  66.         }
  67.         
  68.         String param = bos.toString("utf-8");
  69.         System.out.println("【param】"+param);
  70.                
  71.                  String mode = request.getParameter("hub.mode");
  72.                  String token = request.getParameter("hub.verify_token");
  73.                  String challenge = request.getParameter("hub.challenge");
  74.                  
  75.                  System.out.println(mode);
  76.                  System.out.println(token);
  77.                  System.out.println(challenge);
  78.                  
  79.                  System.out.println(JsonUtil.objToJson(request.getParameterMap()));
  80.                
  81.                 PrintWriter out = response.getWriter();
  82.                 out.print(challenge);  // hub.challenge  原封不动返回,用于校验
  83.                 out.flush();
  84.                 out.close();
  85.         }

  86.         /**
  87.          * Initialization of the servlet. <br>
  88.          *
  89.          * @throws ServletException if an error occurs
  90.          */
  91.         public void init() throws ServletException {
  92.                 // Put your code here
  93.         }

  94. }
复制代码





token 使用Messenger后台生成的token


305.png

然后在Messenger后台  绑定Webhook 回调
527.png

主页事件里至少要选中前两个哦

902.png

我这里为了方便测试,全选了,实际情况要按需选择

验证通过后就可以使用了。
在自己的Messenger  里给 游戏主页发消息看看后台控制台打印的信息


回复

使用道具 举报

游客
回复
您需要登录后才可以回帖 登录 | 立即注册

快速回复 返回顶部 返回列表