Twilio Functions ( 以下 Functions ) は Node が稼働する Serverless 環境です。
2 つの Functions
Functions は 2 種類あります。Twilio コンソールから利用する UI 版 ( Classic ) と、API や CLI でデプロイが可能な API 版 ( Services ) です。UI 版には API がないため Twilio コンソールからの操作のみとなります。それぞれのドキュメントとコンソールは下記となります。
UI 版 Classic
https://jp.twilio.com/docs/runtime/functions
https://jp.twilio.com/console/functions/manage
API 版 Services
https://jp.twilio.com/docs/runtime/functions-assets-api
https://jp.twilio.com/console/functions/overview/services
Node のバージョンについて
Node の EoL に合わせて Functions の Node バージョンも更新されます。
https://jp.twilio.com/docs/runtime/runtime-node-upgrade
バージョンの変更は Twilio Changelog でや弊サイトで案内いたします。
https://www.twilio.com/changelog
UI 版 Classic
・概要についてはこちらをご参照ください。
・Check for valid Twilio signature とは何ですか?
Twilio からのアクセスを保証する仕組みとなります。URL が知られても Twilio からのアクセスだけに制限ができます。詳細はこちらを参照ください。
https://jp.twilio.com/docs/usage/security#validating-requests
・Twilio Functions のサンプルコードはありますか?
SMS 送信や電話発信などのサンプルはこちらで提供しております。GitHub はさらにたくさんのサンプルがあります。
https://jp.twilio.com/docs/runtime/quickstart
https://github.com/twilio-labs/function-templates
・Twilio Functions の制限について
処理は 10 秒以内に callback() を返す必要があります。その他の制限事項は下記をご参照ください。
https://jp.twilio.com/docs/runtime/functions/request-flow#request-limitations
・Twilio Functions の実行の仕組みについて
event に POST/GET の値が入ります。context は環境変数、callback() はエラーや出力を返します。その他仕組みやサンプルについては下記をご参照ください。
https://jp.twilio.com/docs/runtime/functions/invocation
exports.handler = function (context, event, callback) {
const twiml = new Twilio.twiml.MessagingResponse();
return callback(null, twiml);
}
環境変数は Twilio コンソールから設定可能です。
https://jp.twilio.com/console/functions/configure
・npm モジュールのインストール方法
ドキュメントと、npm モジュールをインストールする Twilio コンソールは下記となります。
https://jp.twilio.com/docs/runtime/functions/packages
https://jp.twilio.com/console/functions/configure
標準で入っている twilio モジュールは古いため最新版に更新することを強く推奨します。古いバージョンは Sync の一部オプションや、Verify v2 が利用いただけません。
https://www.npmjs.com/package/twilio
・Private Asset の Functions からの利用方法
Private Assets は Function 以外から利用できないファイルです。利用方法のサンプルコードはこちらをご参照ください。
https://jp.twilio.com/docs/runtime/assets
Assets の制限はこちらをご参照ください。
https://jp.twilio.com/docs/runtime/assets#limitations
・デバッグについて
作成した Functions の画面下に LOGS があり、画面を開いている間は console.log などのログがリアルタイムに表示されます。
https://jp.twilio.com/docs/runtime/functions/debugging
エラーは下記に記録されます。
https://jp.twilio.com/console/debugger
・その他の FAQ について
callback について、JSON で返す、CORS といった話題を扱っています。
https://jp.twilio.com/docs/runtime/functions/faq
API 版 Services
Services は Node.js の Express のようにローカル環境で git 管理やお好きなエディタを利用して開発ができ、Twilio CLI からデプロイ可能なサービスです。
・クイックスタートはこちらをご参照ください。
https://jp.twilio.com/docs/runtime/functions-assets-api/quickstart
Twilio CLI など便利なツールはこちらを参照ください。
https://jp.twilio.com/docs/labs/serverless-toolkit
・各種 API 情報
https://jp.twilio.com/docs/runtime/functions-assets-api/api/service
・制限事項について
作成可能なサービス数や Asset の数などはこちらをご参照ください。UI 版 Assets と別になります。
https://jp.twilio.com/docs/runtime/functions-assets-api
・GitHub Actions と CI/CD の利用例
https://www.twilio.com/blog/an-introduction-to-ci-cd-for-twilio-functions-using-github-actions
Twilio Functions で最新の Sync を使う方法
Twilio Functions ( 以下 Functions ) で Sync を利用する場合は最新の twilio-sync の導入をお勧めいたします。Functions 標準の Sync 利用は twilio-node となりますが、Sync のバージョンが古い場合があるためです。
twilio-sync の Functions へのインストール方法は下記をご参照ください。
https://jp.twilio.com/docs/runtime/functions/packages
twilio-sync のバージョンは下記をご参照ください。
https://www.twilio.com/docs/sync/javascript-sdk-changelog#version-history-changelog
twilio-sync は twilio-node と書き方が異なる部分があります。ご利用方法は下記をご参照ください。
https://www.twilio.com/docs/sync/quickstart/js
https://www.twilio.com/docs/sync
https://www.npmjs.com/package/twilio-sync
0 コメント