whatsapp-cloud-api
Whatsapp Cloud API を使用してボットを作成し、メッセージを送受信するための Node.js ライブラリです。
組み込みの Typescript 宣言が含まれています。
このプロジェクトは現在アーカイブされています。詳細はこちらをご覧ください。
npm の使用:
npm i whatsapp-cloud-api
糸の使用:
yarn add whatsapp-cloud-api
import { createBot } from 'whatsapp-cloud-api' ;
// or if using require:
// const { createBot } = require('whatsapp-cloud-api');
( async ( ) => {
try {
// replace the values below
const from = 'YOUR_WHATSAPP_PHONE_NUMBER_ID' ;
const token = 'YOUR_TEMPORARY_OR_PERMANENT_ACCESS_TOKEN' ;
const to = 'PHONE_NUMBER_OF_RECIPIENT' ;
const webhookVerifyToken = 'YOUR_WEBHOOK_VERIFICATION_TOKEN' ;
// Create a bot that can send messages
const bot = createBot ( from , token ) ;
// Send text message
const result = await bot . sendText ( to , 'Hello world' ) ;
// Start express server to listen for incoming messages
// NOTE: See below under `Documentation/Tutorial` to learn how
// you can verify the webhook URL and make the server publicly available
await bot . startExpressServer ( {
webhookVerifyToken ,
} ) ;
// Listen to ALL incoming messages
// NOTE: remember to always run: await bot.startExpressServer() first
bot . on ( 'message' , async ( msg ) => {
console . log ( msg ) ;
if ( msg . type === 'text' ) {
await bot . sendText ( msg . from , 'Received your text message!' ) ;
} else if ( msg . type === 'image' ) {
await bot . sendText ( msg . from , 'Received your image!' ) ;
}
} ) ;
} catch ( err ) {
console . log ( err ) ;
}
} ) ( ) ;
他のメッセージ タイプの送信 (詳細は API リファレンスを参照):
// Send image
const result = await bot . sendImage ( to , 'https://picsum.photos/200/300' , {
caption : 'Random jpg' ,
} ) ;
// Send location
const result = await bot . sendLocation ( to , 40.7128 , - 74.0060 , {
name : 'New York' ,
} ) ;
// Send template
const result = await bot . sendTemplate ( to , 'hello_world' , 'en_us' ) ;
カスタマイズされた Express サーバー (詳細は下記を参照):
import cors from 'cors' ;
// Create bot...
const bot = createBot ( ... ) ;
// Customize server
await bot . startExpressServer ( {
webhookVerifyToken : 'my-verification-token' ,
port : 3000 ,
webhookPath : `/custom/webhook` ,
useMiddleware : ( app ) => {
app . use ( cors ( ) ) ,
} ,
} ) ;
他のメッセージ タイプをリッスンする (詳細は API リファレンスを参照):
const bot = createBot ( ... ) ;
await bot . startExpressServer ( { webhookVerifyToken } ) ;
// Listen to incoming text messages ONLY
bot . on ( 'text' , async ( msg ) => {
console . log ( msg ) ;
await bot . sendText ( msg . from , 'Received your text!' ) ;
} ) ;
// Listen to incoming image messages ONLY
bot . on ( 'image' , async ( msg ) => {
console . log ( msg ) ;
await bot . sendText ( msg . from , 'Received your image!' ) ;
} ) ;
デフォルトでは、whatsapp 関連のリクエストのエンドポイントは/webhook/whatsapp
になります。これは、ローカルの URL がhttp://localhost/webhook/whatsapp
になることを意味します。
リバース プロキシを使用して、サーバーを公開できます。この例としては ngrok があります。
詳細については、チュートリアルをご覧ください。
上記の実装では、受信メッセージをリッスンする Express サーバーが作成されます。将来的には他のタイプのサーバーをサポートする計画があるかもしれません (PR は大歓迎です! :))。
次のようにポートを変更できます。
await bot . startExpressServer ( {
port : 3000 ,
} ) ;
デフォルトでは、すべてのリクエストはPOST|GET /webhook/whatsapp
エンドポイントによって処理されます。これは次のように変更できます。
await bot . startExpressServer ( {
webhookPath : `/custom/webhook` ,
} ) ;
注:先頭の/
覚えておいてください。つまり、 custom/whatsapp
使用しないでください。代わりに/custom/whatsapp
使用してください。
アプリケーションで Express サーバーをすでに実行している場合は、以下のように使用することで新しいサーバーの作成を回避できます。
// your code...
import express from 'express' ;
const app = express ( ) ;
...
// use the `app` variable below:
await bot . startExpressServer ( {
app ,
} ) ;
ミドルウェアを追加するには:
import cors from 'cors' ;
await bot . startExpressServer ( {
useMiddleware : ( app ) => {
app . use ( cors ( ) ) ,
} ,
} ) ;
完全にカスタマイズされたセットアップ:
import cors from 'cors' ;
await bot . startExpressServer ( {
webhookVerifyToken : 'my-verification-token' ,
port : 3000 ,
webhookPath : `/custom/webhook` ,
useMiddleware : ( app ) => {
app . use ( cors ( ) ) ,
} ,
} ) ;
on()
リスナーこのライブラリは単一プロセスの pubsub を使用します。つまり、分散 Kubernetes クラスターなどのマルチインスタンス クラスターにデプロイしている場合は、うまく機能しません。将来的には、外部ストレージ、たとえば redis に保存できる pubsub リファレンスをエクスポート/サポートする計画があるかもしれません (PR は大歓迎です! :))。
# install npm modules
npm i
# eslint
npm run lint
# typescript check
npm run ts-check
# test
# # Read 'Local Testing' below before running this
npm t
# build
npm run build
プロジェクトのルートに .env ファイルを作成します。
FROM_PHONE_NUMBER_ID=""
ACCESS_TOKEN=""
VERSION=""
TO=""
WEBHOOK_VERIFY_TOKEN=""
WEBHOOK_PATH=""
node-telegram-bot-api からインスピレーションを得たライブラリ API。
すべての PR がオープンです。