[英]Facebook messenger bot not sending messages (Python/Django)
我已经按照本教程实施了一个Facebook Messenger机器人,该机器人只是回显您键入的内容。 它与Facebook挂钩,但我无法使其超出工作范围,并且我找不到问题。 你能帮我么? 到目前为止,这是代码(与本教程中的代码相比,进行了少量修改)。
class BotsView(generic.View):
def get(self, request, *args, **kwargs):
if self.request.GET.get('hub.verify_token') == '1111111111':
return HttpResponse(self.request.GET.get('hub.challenge'))
else:
return HttpResponse('Error, invalid token')
def post_facebook_message(fbid, recevied_message):
post_message_url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<access-token>'
response_msg = json.dumps({"recipient":{"id":fbid}, "message":{"text":recevied_message}})
requests.post(post_message_url, headers={"Content-Type": "application/json"},data=response_msg)
return HttpResponse()
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return generic.View.dispatch(self, request, *args, **kwargs)
def post(self, request, *args, **kwargs):
# Converts the text payload into a python dictionary
incoming_message = json.loads(self.request.body)
# Facebook recommends going through every entry since they might send
# multiple messages in a single call during high load
for entry in incoming_message['entry']:
for message in entry['messaging']:
# Check to make sure the received call is a message call
# This might be delivery, optin, postback for other events
if message.has_key('message'):
# Print the message to the terminal
# pprint(message)
# Assuming the sender only sends text. Non-text messages like stickers, audio, pictures
# are sent as attachments and must be handled accordingly.
post_facebook_message(message['sender']['id'], message['message']['text'])
return HttpResponse()
尝试放置整个功能
def post_facebook_message(fbid, recevied_message):
....
在BotsView类之外。 如果将其保留在类中,则必须将“ self”作为其第一个参数,并且必须在类中以以下方式访问它们:
self.post_facebook_message(.....)
但是,将此功能放在Django View类中可能不是最好的选择。
ps-谢谢您,我将更新教程以明确说明这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.