whatsapp-cloud-api
是一個 Node.js 函式庫,用於建立機器人並使用 Whatsapp Cloud API 發送/接收訊息。
包含內建 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' ) ;
客製化的快遞伺服器(閱讀下面的更多內容):
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 就是一個例子。
您可以在教程中閱讀更多內容。
上面的實作為您建立了一個快速伺服器,透過它偵聽傳入的訊息。將來可能計劃支援其他類型的伺服器(歡迎 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 叢集)上,它將無法正常運作。將來,可能計劃導出/支援可以儲存在外部儲存中的 pubsub 引用,例如 redis(歡迎 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=""
庫 API 的靈感來自於 node-telegram-bot-api。
所有 PR 都是開放的。