LoginSignup
3
0

More than 1 year has passed since last update.

Vonageを用いてFacebook Messengerにメッセージを送ーる(Python3.9、Windows 10)

Last updated at Posted at 2022-12-18

はじめに

Vonageを用いてFacebook Messengerにメッセージを送ってみます

開発環境

  • Windows 10 PC
  • Python 3.9

メッセージを送る

1.アカウントを作成し、ログインします

2.ダッシュボードはこちらです

3.Facebook Messengerをサンドボックスに追加します

image.png

4.リンクをクリックしてメッセージでパスフレーズを送ります

image.png

5.IDが返ってくるので一応メモ

image.png

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'}

9.メッセンジャーにメッセージがきました!
image.png

画像を送る

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())

image.png

ファイルを送る

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())

image.png

音声を送る

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())

image.png

ビデオを送る

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())

image.png

お疲れ様でした

参考文献

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0