Facebook messenger机器人代码出错

时间:2016-05-24 21:11:53

标签: python google-app-engine facebook-messenger

我正在尝试关注快速入门Facebook Messenger Bot代码(可在此处找到:https://developers.facebook.com/docs/messenger-platform/quickstart

我收到了这个错误:

{“error”:{“message”:“(#100)param收件人必须为非空。”,“type”:“OAuthException”,“code”:100,“fbtrace_id”:“ALtKsX4nJtX”} }

有什么想法吗?好像我正在回复正确的回复,但出于某种原因Facebook不接受它?感谢

代码如下(python / appengine):

import webapp2
import json
import urllib
from google.appengine.api import urlfetch

class MainHandler(webapp2.RequestHandler):
    print "hello"

FB_MESSENGER_ACCESS_TOKEN = ""

def respond_FB(sender_id, text):
    json_data = {
        "recipient": {"id": sender_id},
        "message": {"text": text + " to you!"}
    }
    url = "https://graph.facebook.com/v2.6/me/messages?access_token="+FB_MESSENGER_ACCESS_TOKEN
    print("json_data", json_data)
    data = urllib.urlencode(json_data)
    response = urlfetch.fetch(url = url, payload = data, method = urlfetch.POST, headers = {"Content-Type": "application/json"})
    print(response.content)

class FacebookMessenger(webapp2.RequestHandler):
    def get(self):
        if (self.request.get("hub.verify_token") == 'tomstoken'):
            return self.response.write(self.request.get("hub.challenge"))
        return self.response.write('Error, wrong validation token')


    def post(self):
        body = self.request.body
        messaging_events = json.loads(body.decode("utf-8"))
        sender_id = messaging_events["entry"][0]["messaging"][0]["sender"]["id"]
        message = messaging_events["entry"][0]["messaging"][0]["message"]["text"]
        respond_FB(sender_id, message)
        return self.response.write('Received.')

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/webhook*', FacebookMessenger)
], debug=True)

1 个答案:

答案 0 :(得分:1)

我刚刚使用您的函数requests进行了测试,因为我没有谷歌应用引擎api所以我使用了response = requests.post(url, json=data)

data

如果json_data 替换为 json_data,则一切顺利

不知道为什么你需要编码gcloud,这不是网址