[Twilio+Node.js] Twilio API を使用した SMS 送信

[Twilio+Node.js] Twilio API を使用した SMS 送信

Twilio API を使用して Node.js アプリケーションから SMS を送信する手順を紹介します。 Twilio アカウントが作成済みである想定です。
参考ページ: https://www.twilio.com/docs/messaging/quickstart/node

作業環境

バージョン

OS: Windows 11 (23H2)
node: 22.13.1
npm: 10.9.2
node_modules/twilio: 5.4.5

Twilio CLI のインストール

Scoop を使用して Windows 環境に Twilio CLI をインストールします。 Powershell を管理者権限で起動し、次のコマンドを実行します。

$ scoop bucket add twilio-scoop https://github.com/twilio/scoop-twilio-cli
$ scoop install twilio
Installing 'twilio' (5.22.11) [64bit] from 'twilio-scoop' bucket
Loading twilio-win32-x64.tar.xz from cache
Checking hash of twilio-win32-x64.tar.xz ... ok.
Extracting twilio-win32-x64.tar.xz ... done.
Linking ~\scoop\apps\twilio\current => ~\scoop\apps\twilio\5.22.11
Creating shim for 'twilio'.
Running post_install script...
*************************************************************************
*                                                                       *
* To get started, please create a twilio-cli profile:                   *
*                                                                       *
*     twilio login                                                      *
*                                                                       *
*     OR                                                                *
*                                                                       *
*     twilio profiles:create                                            *
*                                                                       *
*************************************************************************

 » If you’re using autocomplete, you’ll need to run 'twilio autocomplete' after the install and then open a new terminal window. The CLI needs to re-build its cache.
done.
'twilio' (5.22.11) was installed successfully!

twilio login コマンドでログインします。

$ twilio login
? The Account SID for your Twilio Account or Subaccount: ****** # Account SID を入力します
? Your Twilio Auth Token for your Twilio Account or Subaccount: ****** # Auth Token を入力します
? Shorthand identifier for your profile: nodejs-sample # 任意の名前を入力します

Node.js のインストール

Node.js をインストールします。バージョンが 14 以降であることを確認します。

$ node --version
v22.13.1

Twilio モジュールのインストール

npm で Twilio モジュールをインストールします。

$ npm install twilio

SMS の送信

SMS 送信を行うスクリプトを作成します。

app.js

const twilio = require("twilio");

const accountSid = "****" // Account SID を入力します
const authToken = "****" // Auth Token を入力します
const client = twilio(accountSid, authToken);

async function createMessage() {
    const message = await client.messages.create({
        body: "あのイーハトーヴォのすきとおった風",
        from: "****", // Twilio で購入した番号を入力します
    to: "****", // 送信先の電話番号を入力します
});

    console.log(message.body);
}

createMessage();

実行し、 SMS が届くことを確認します。

$ node app.js
あのイーハトーヴォのすきとおった風

test-twilio-js-sms

おわりに

Twilio API を使用することで、非常に簡単に SMS 送信を実装することができました。

次回、自動返信機能の実装については こちら

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy