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
คุณสามารถใช้พร็อกซีย้อนกลับเพื่อทำให้เซิร์ฟเวอร์พร้อมใช้งานแบบสาธารณะ ตัวอย่างนี้คืองรก
คุณสามารถอ่านเพิ่มเติมเกี่ยวกับบทช่วยสอน
การใช้งานข้างต้นจะสร้างเซิร์ฟเวอร์ด่วนสำหรับคุณเพื่อรับฟังข้อความที่เข้ามา อาจมีแผนการรองรับเซิร์ฟเวอร์ประเภทอื่นในอนาคต (ยินดีต้อนรับ PR! :))
คุณสามารถเปลี่ยนพอร์ตได้ดังนี้:
await bot . startExpressServer ( {
port : 3000 ,
} ) ;
ตามค่าเริ่มต้น คำขอทั้งหมดจะได้รับการจัดการโดยจุดสิ้นสุด POST|GET /webhook/whatsapp
คุณสามารถเปลี่ยนแปลงได้ดังต่อไปนี้:
await bot . startExpressServer ( {
webhookPath : `/custom/webhook` ,
} ) ;
หมายเหตุ: จำคำนำหน้า /
; คือไม่ใช้ custom/whatsapp
; /custom/whatsapp
แทน
หากคุณใช้งานเซิร์ฟเวอร์ด่วนในแอปพลิเคชันของคุณอยู่แล้ว คุณสามารถหลีกเลี่ยงการสร้างเซิร์ฟเวอร์ใหม่ได้โดยใช้เซิร์ฟเวอร์ดังกล่าวดังต่อไปนี้:
// 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=""
Library API ได้รับแรงบันดาลใจจาก node-telegram-bot-api
PR ใด ๆ และทั้งหมดเปิดอยู่