FB Messenger webhookクイックスタートページでPythonで何も見つかりませんでした。 (https://developers.facebook.com/docs/messenger-platform/quickstart)新しいFB Messengerウェブフック用のPythonポートはありますか?
1
A
答えて
3
私は先に進んで自分自身を移植しました。私は富を分かち合うべきだと思った。 "あなたに!" を持つすべてのメッセージに、このスニペットの応答:
import json
import requests
from django.views.decorators.csrf import csrf_exempt
FB_MESSENGER_ACCESS_TOKEN = "<Your Access Token>"
def respond_FB(sender_id, text):
json_data = {
"recipient": {"id": sender_id},
"message": {"text": text + " to you!"}
}
params = {
"access_token": FB_MESSENGER_ACCESS_TOKEN
}
r = requests.post('https://graph.facebook.com/v2.6/me/messages', json=json_data, params=params)
print(r, r.status_code, r.text)
#this allows the requst to come in without requiring CSRF token
@csrf_exempt
def fb_webhook(request):
if request.method == "GET":
if (request.GET.get('hub.verify_token') == 'this_is_a_verify_token_created_by_sean'):
return HttpResponse(request.GET.get('hub.challenge'))
return HttpResponse('Error, wrong validation token')
if request.method == "POST":
body = request.body
print("BODY", body)
messaging_events = json.loads(body.decode("utf-8"))
print("JSON BODY", body)
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 HttpResponse('Received.')
1
私はFacebookのメッセンジャープラットフォーム用のPythonクライアントを書いた:messengerbot
1
これはfbmq libraryそれを使用してPythonのサンプルです私の作品:
エコー例:
from flask import Flask, request
from fbmq import Page
page = fbmq.Page(PAGE_ACCESS_TOKEN)
@app.route('/webhook', methods=['POST'])
def webhook():
page.handle_webhook(request.get_data(as_text=True))
return "ok"
@page.handle_message
def message_handler(event):
page.send(event.sender_id, event.message_text)
関連する問題
- 1. Windows Live Messenger Connect v5 C#SDKはありますか?
- 2. Android:PCポートはありますか?
- 3. PHP用のHtmlUnitポートはありますか?
- 4. ZF2用の新しいバージョンのZend_Toolはありますか?
- 5. 新しいFirefox 13用のDev Modeプラグインはありますか?
- 6. yahoo messenger lib for python?
- 7. Dartにsocket.ioポートがありますか?
- 8. mailchimpウェブフック
- 9. FBコメントは非FBコメントと混在していますか?
- 10. まだPython 2.6とPython 3の違いはありますか?
- 11. SSL(ポート443)との問題点とFB
- 12. SWT 3.6からWPFのポートはありますか?
- 13. SqlConnection.CreateCommandと新しいSqlCommandの違いはありますか?
- 14. Lego Mindstorm用のRubyまたはPythonインタプリタはありますか?
- 15. ID3メタデータ用のPerlまたはPythonライブラリはありますか?
- 16. JavaのMatcherクラスのPHPポートはありますか?
- 17. Androidのポート番号のレジストリはありますか?
- 18. Pythonでのファイル管理用のライブラリ/モジュールはありますか?
- 19. ディープラーニング用のPythonライブラリまたはAPIはありますか?
- 20. 最新のブラウザ用のCSS用のリセットライブラリはありますか?
- 21. Dockerはポート転送が正しくありませんか?
- 22. 標準の代替HTTPSポートはありますか?
- 23. スタンフォードJavascript暗号化ライブラリのPHPポートはありますか?
- 24. SolarisでIPv6をサポートするJava用の新しいライブラリはありますか?
- 25. SQL Server 2012(denali)用の新しいバージョンのRDLスキーマはありますか?
- 26. 中級または進学の学習者用の新しいiPhone SDKの新刊はありますか
- 27. Python 3用に動作するOAuthライブラリはありますか?
- 28. 新しいオブジェクトのショートカットキーはありますか?
- 29. メイベックレポの新しいウィケットはどこにありますか?
- 30. Foxproの新しいODBCオプションはありますか?
ありがとうSean! 'r = requests.post( 'https://graph.facebook.com/v2.6/me/messages'、json = json_data、params = params)の' urllib'に相当するものは何でしょうか? –
このサイトhttp://buddylindsey.com/basic-urllib-get-and-post-with-and-without-data/は、このテーマに関する情報を提供しています。私は最終的なコードが次のようになると思われます。 data = urllib.urlencode(json_data); u = urllib.urlopen( "https://graph.facebook.com/v2.6/me/messages?access_token = [access_token]"、data); – Sean
ありがとうございます!私はGAE上でコードを実行しているので、 '要求'は使用できません。あなたが指摘したコードを使用したが、何とか「Bad Request 400」という結果に終わった。どんなアイデアが間違っているのでしょうか? –