Secreto31126 / whatsapp-api-js

A TypeScript server agnostic Whatsapp's Official API framework
MIT License
128 stars 31 forks source link

Send message to multiple phone numbers #300

Closed angelopedroso closed 4 months ago

angelopedroso commented 5 months ago

The title explain my doubt 😁.

angelopedroso commented 5 months ago

Do you may create a discord server btw?

Secreto31126 commented 5 months ago

I'm gonna guess you found the answer. For future reference, here's a link to the broadcastMessage method.

About the Discord server, I don't think I have the time to manage it, especially right now. I am however always happy to answer any question that raises here at GitHub :)

If the repo ever gets two or three times the attention it has now, or GitHub issues/discussions becomes unbearably disorganized, I will think about it.

angelopedroso commented 5 months ago

I'm gonna guess you found the answer. For future reference, here's a link to the broadcastMessage method.

About the Discord server, I don't think I have the time to manage it, especially right now. I am however always happy to answer any question that raises here at GitHub :)

If the repo ever gets two or three times the attention it has now, or GitHub issues/discussions becomes unbearably disorganized, I will think about it.

I see, and about image messages, do u know whether it's allow base64 entry or just url/id method?

Secreto31126 commented 5 months ago

Unfortunately, WhatsApp doesn't support base64 images as far as I know. You can see the full list of supported media types here.

angelopedroso commented 5 months ago

Send message return that response:

{
  messaging_product: 'whatsapp',
  contacts: [ { input: 'example', wa_id: 'example' } ],        
  messages: [
    {
      id: 'wamid.example=='
    }
  ]
}

But number doesn't receive the message.

Secreto31126 commented 5 months ago

I'm gonna guess those "examples" are edited to hide information.

Two things to keep in mind:

  1. To send a message via the API, the user you are writing to must have initiated conversation with the bot in the last 24 hours. That's an anti spam feature from the API itself, so no way to get around it (unless you use a template). You may read more about it at the official documentation.

  2. Before sending messages, it's strongly recommended handling the post requests from Meta to your server, which you can use to log the Whatsapp.on.status webhook. Here you shall see any errors that may happen in your code or at the API side.

A super naive and probably not working snippet of how to use it:

// idk enough NestJS to help with
// this part of the code :/
function on_post(req, res) {
    wppAPI.post(req.body);
}

wppAPI.on.status = console.log;
angelopedroso commented 5 months ago

Ok, i've migrate to express, i'm use the middleware, but return me this error:

export abstract class WhatsAppAPIMiddleware extends WhatsAppAPI {
                                                    ^

TypeError: Class extends value #<Object> is not a constructor or null
    at Object.<anonymous> (C:\Users\user\Desktop\whatsapp\ap\node_modules\whatsapp-api-js\src\middleware\globals.ts:7:53)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Object.S (C:\Users\user\Desktop\whatsapp\ap\node_modules\tsx\dist\cjs\index.cjs:1:1292)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (C:\Users\user\Desktop\whatsapp\ap\node_modules\whatsapp-api-js\src\middleware\express.ts:1:39)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Object.S (C:\Users\user\Desktop\whatsapp\ap\node_modules\tsx\dist\cjs\index.cjs:1:1292)

Node.js v20.11.0

Code:

import express from 'express'
import cors from 'cors'

import { routes } from './routes'

import fileUpload from 'express-fileupload'
import { envs } from '@/utils/env'

import WhatsAppAPI from 'whatsapp-api-js/middleware/express'

const app = express()

export const Whatsapp = new WhatsAppAPI({
  token: envs.WHATSAPP_TOKEN,
  appSecret: envs.WHATSAPP_APP_SECRET,
  webhookVerifyToken: envs.VERIFY_TOKEN,
})

app.use(
  cors({
    origin: envs.WEBSITE_URL,
  }),
)

app.use(fileUpload())

app.use(express.json())

app.use(routes)

app.listen(envs.PORT, () => {
  console.log('live')
})

// Set the callback
Whatsapp.on.message = ({ from, phoneID }) => {
  console.log(`Message from ${from} to bot ${phoneID}`)
}

// Remove the callback
Whatsapp.on.message = undefined

Version: 2.6.0

Secreto31126 commented 5 months ago

I'm unfortunately unable to debug this issue at least until the 25th. The best I can give you right now is a few tips in what might be breaking:

  1. Check you are using "type": "module" in package.json

  2. Check out the express middleware documentation here, especially handle_post and handle_get methods

  3. Consider not using the middleware for now. The code is quite simple for this class, so it might be easier for your case to just copy paste from the source code

Last, but not least, keep in mind the line of code to remove the handler is not necessary in your case, it's just an example on how to disable the callback.

I'm sorry I can't help you fix this issue any further. I won't be able to use my notebook for a few weeks, and debugging code on my phone is painfully difficult :)

angelopedroso commented 4 months ago

Is there a way to create button of type quick_reply? Like ActionButtons, but quick_reply type instead of interactive

Secreto31126 commented 4 months ago

If you are talking about the Template's button components, then yes, you can use any of the ButtonComponent, and more specifically the PayloadComponent for subtype quick_reply.

angelopedroso commented 4 months ago

Last doubt (maybe). Do you know whether exist a delay between send a template message and after that send a normal message? To allow the bot sends a normal message after sending a template.

Secreto31126 commented 4 months ago

In the API's documentation, the proposed solution in order to secure the messages are sent in the right order is listening to the status webhook (Whatsapp.on.status in the framework). The logic goes as follow:

Send message (Template) -> Await and store message ID from response -> Keep listening messages status until you get message ID === the stored id && status === "delivered" -> Send the next message (Text)

However, IIRC, you can't send freeform messages in a bussiness initiated conversation, only after receiving an user response. If you are in the 24 hours window it will work just fine.

angelopedroso commented 4 months ago

About send freeform messages, you do. But you need to send a template before that, 'cause I've sent a template and sometimes it sends a freeform message, even if the user doesnt send a message.

Secreto31126 commented 4 months ago

Hello there,

I'm going to close this issue as solved/duplicated. Any updates on the middleware's bug should be sent to #306 as I have been doing some progress there and it's neater to keep everything in the same place.

If you have any further question regarding the library, the API or a general code problem, please write them in Discussions, which is the proper place for such questions. It's not Discord, but it helps keeping the issues and questions sorted :)

Thanks for your understanding!