はじめに
Vonageを用いてFacebook Messengerにメッセージを送ってみます
開発環境
- Windows 10 PC
- Python 3.9
メッセージを送る
1.アカウントを作成し、ログインします
2.ダッシュボードはこちらです
3.Facebook Messengerをサンドボックスに追加します
4.リンクをクリックしてメッセージでパスフレーズを送ります
5.IDが返ってくるので一応メモ
6.curlからAPIを叩き、メッセージを送る場合の例です
curl -X POST https://messages-sandbox.nexmo.com/v1/messages \
-u 'c0e65d50:LC7RLPgvbYvQB1On' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d '{
"from": "VONAGE_SANDBOX_ID",
"to": "FB_RECIPIENT_ID",
"message_type": "text",
"text": "This is a Facebook Messenger Message sent from the Messages API",
"channel": "messenger"
}'
7.Pythonから叩いてみましょう!
fb_send_message.py
import requests
import json
url = "https://messages-sandbox.nexmo.com/v1/messages"
headers = {"Content-Type": "application/json"}
data = {"from": "VONAGE_SANDBOX_ID",
"to": "FB_RECIPIENT_ID",
"message_type": "text",
"text": "This is a Facebook Messenger Message sent from the Messages API",
"channel": "messenger"}
response = requests.post(url,data=json.dumps(data), headers=headers, auth=("ID","PASS"))
print(response.json())
8.UUIDが返ってきます
{'message_uuid': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'}
画像を送る
fb_send_image.py
import requests
import json
url = "https://messages-sandbox.nexmo.com/v1/messages"
headers = {"Content-Type": "application/json"}
data = {"from": "VONAGE_SANDBOX_ID",
"to": "FB_RECIPIENT_ID",
"message_type": "image",
"image": {
"url": "https://www.gachimoto.com/media/1ea870daf61ee0c98d717b8d004d8d7eb059a1098d4e551bfcf6d6bd.png"
},
"channel": "messenger"}
response = requests.post(url,data=json.dumps(data), headers=headers, auth=("ID","PASS"))
print(response.json())
ファイルを送る
fb_send_file.py
import requests
import json
url = "https://messages-sandbox.nexmo.com/v1/messages"
headers = {"Content-Type": "application/json"}
data = {"from": "VONAGE_SANDBOX_ID",
"to": "FB_RECIPIENT_ID",
"message_type": "file",
"file": {
"url": "https://www.pref.kumamoto.jp/uploaded/attachment/175083.pdf"
},
"channel": "messenger"}
response = requests.post(url,data=json.dumps(data), headers=headers, auth=("ID","PASS"))
print(response.json())
音声を送る
fb_send_audio.py
import requests
import json
url = "https://messages-sandbox.nexmo.com/v1/messages"
headers = {"Content-Type": "application/json"}
data = {"from": "VONAGE_SANDBOX_ID",
"to": "FB_RECIPIENT_ID",
"message_type": "audio",
"audio": {
"url": "https://soundeffect-lab.info/sound/voice/mp3/line-girl1/line-girl1-ohayougozaimasu1.mp3"
},
"channel": "messenger"}
response = requests.post(url,data=json.dumps(data), headers=headers, auth=("ID","PASS"))
print(response.json())
ビデオを送る
fb_send_video.py
import requests
import json
url = "https://messages-sandbox.nexmo.com/v1/messages"
headers = {"Content-Type": "application/json"}
data = {"from": "VONAGE_SANDBOX_ID",
"to": "FB_RECIPIENT_ID",
"message_type": "video",
"video": {
"url": "https://pixabay.com/ja/videos/download/video-6970_tiny.mp4"
},
"channel": "messenger"}
response = requests.post(url,data=json.dumps(data), headers=headers, auth=("ID","PASS"))
print(response.json())
お疲れ様でした
参考文献