设为首页
收藏本站
收藏
任务
排行
道具
勋章
官方Q群
群号:814298516
简介:为国内Facebook开发者提供的交流群,以Facebook instant game为重点,涵盖其他Facebook开发、推广和运营交流
综合讨论区
新手专区
技术讨论区
设置与配置区
运营推广区
游戏\应用展示
源码与资源专区
VR游戏\应用
论坛
BBS
搜索
搜索
热搜:
活动
交友
discuz
Hi~
登录
或
注册
本版
帖子
用户
Facebook开发者社区
»
论坛
›
新手专区
›
新手专区
›
FB小游戏开发准备五 (Java 创建 Webhook 服务 提供Mess ...
返回列表
发新帖
查看:
2436
|
回复:
0
FB小游戏开发准备五 (Java 创建 Webhook 服务 提供Messenger 智能助手体验)
[复制链接]
破晓
破晓
当前离线
积分
149
6
主题
10
帖子
149
积分
注册会员
注册会员, 积分 149, 距离下一级还需 51 积分
注册会员, 积分 149, 距离下一级还需 51 积分
积分
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
package com.airmyth.servlet;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.airmyth.util.JsonUtil;
@WebServlet("/webhook")
public class WebhookServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public WebhookServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
// 使用Messenger后台生成的token
private String faceToken = "**************tn3ADVR5nFEZA036wnFY94lt*************";
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
ServletInputStream sis = request.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
int bytes = 0;
byte[] buffer = new byte[8192];
while ((bytes = sis.read(buffer, 0, 8192)) != -1)
{
dos.write(buffer, 0, bytes);
}
String param = bos.toString("utf-8");
System.out.println("【param】"+param);
String mode = request.getParameter("hub.mode");
String token = request.getParameter("hub.verify_token");
String challenge = request.getParameter("hub.challenge");
System.out.println(mode);
System.out.println(token);
System.out.println(challenge);
System.out.println(JsonUtil.objToJson(request.getParameterMap()));
PrintWriter out = response.getWriter();
out.print(challenge); // hub.challenge 原封不动返回,用于校验
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
复制代码
token 使用Messenger后台生成的token
然后在Messenger后台 绑定Webhook 回调
主页事件里至少要选中前两个哦
我这里为了方便测试,全选了,实际情况要按需选择
验证通过后就可以使用了。
在自己的Messenger 里给 游戏主页发消息看看后台控制台打印的信息
回复
使用道具
举报
返回列表
发新帖
游客
回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
|
立即注册
发表回复
快速回复
返回顶部
返回列表