フェイルオーバーでFacebookメッセージを送信

この例では、Facebook Messenger メッセージを送信し、SMS 送信にフェイルオーバーします。ワークフローオブジェクトのメッセージオブジェクトは、ユースケースに合わせて任意の順番で配置することができます。各メッセージオブジェクトにはフェイルオーバーオブジェクトが含まれている必要があります。

このトピックに関するステップバイステップのガイドについては、チュートリアルをお読みください。 フェイルオーバーによるFacebookメッセージの送信.

以下の変数が、便利な方法で必要な値に設定されていることを確認してください:

キー説明
VONAGE_APPLICATION_ID

The Vonage Application ID.

FROM_NUMBER

Replace with number you are sending from. E.g. 447700900002

TO_NUMBER

Replace with the number you are sending to. E.g. 447700900001

FB_SENDER_ID

Your Page ID. The FB_SENDER_ID is the same as the to.id value you received in the inbound messenger event on your Inbound Message Webhook URL.

FB_RECIPIENT_ID

The PSID of the user you want to reply to. The FB_RECIPIENT_ID is the PSID of the Facebook User you are messaging. This value is the from.id value you received in the inbound messenger event on your Inbound Message Webhook URL.

注: 先頭の + または 00 電話番号を入力する場合は、447700900000のように国番号から入力してください。

前提条件

申込書をお持ちでない場合は、ひとつ作るウェブフックの設定 もご確認ください。

コードを書く

send-message-with-failover.sh に以下を追加する:

curl -X POST https://api.nexmo.com/v0.1/dispatch \
  -H 'Authorization: Bearer '$JWT\
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -d $'{
    "template":"failover",
    "workflow": [
      {
        "from": { "type": "messenger", "id": '$FB_SENDER_ID' },
        "to": { "type": "messenger", "id": '$FB_RECIPIENT_ID' },
        "message": {
          "content": {
            "type": "text",
            "text": "This is a Facebook Messenger Message sent via the Dispatch API"
          }
        },
        "failover":{
          "expiry_time": 600,
          "condition_status": "read"
        }
      },
      {
        "from": {"type": "sms", "number": '$FROM_NUMBER'},
        "to": { "type": "sms", "number": '$TO_NUMBER'},
        "message": {
          "content": {
            "type": "text",
            "text": "This is an SMS sent via the Dispatch API"
          }
        }
      }
    ]
  }'

全文を見る

コードを実行する

このファイルをあなたのマシンに保存し、実行する:

bash send-message-with-failover.sh

試してみる

コードを実行すると、Facebook Messenger経由でメッセージを送信しようとします。これは失敗し、メッセージがSMSで宛先の番号に送信されます。