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

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

# 開発環境
- Windows 10 PC
- Python 3.9

# メッセージを送る

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

https://www.vonagebusiness.jp/

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

https://dashboard.nexmo.com/messages/sandbox

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/e471b0cc-d83e-d1a3-958b-1fad91b50a1f.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/f7cea25c-fc82-f3fa-5186-aa9e8db61705.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/e192ebe2-c013-0e61-7328-0f5909c64349.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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/55d204b2-9112-e349-ec92-0e78d171ecb3.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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/294f6efe-bdd2-4bde-4cdc-e5ad1f266673.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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/17ab7441-6ac7-bff3-c5c9-fb319aed0561.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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/9a848681-b284-7f66-824a-24b1efd97707.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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/19096105-a40c-c4eb-cc9b-4a17bba3e499.png)

お疲れ様でした

# 参考文献

https://developer.vonage.com/messages/code-snippets/messenger/send-file

https://dashboard.nexmo.com/getting-started/meetings-api