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 都是开放的。