tawn33y / whatsapp-cloud-api

A Node.js library for creating bots and sending/receiving messages using the Whatsapp Cloud API.
https://www.npmjs.com/package/whatsapp-cloud-api
GNU General Public License v3.0
187 stars 51 forks source link

possible error when adding routes #47

Closed KevinJoseph closed 1 year ago

KevinJoseph commented 1 year ago

Hi, I'm starting the following:

const { createBot } = require('whatsapp-cloud-api'); const axios = require('axios'); const express = require('express');

const app = express();

const from = process.env.WHATSAPP_PHONE_NUMBER_ID; const token = process.env.WHATSAPP_TOKEN; const webhookVerifyToken = process.env.VERIFY_TOKEN;

// Create a bot that can send messages const bot = createBot(from, token); (async function() { await bot.startExpressServer({ webhookVerifyToken: process.env.VERIFY_TOKEN, port: process.env.PORT || 2040, webhookPath: /webhook, }); })();

app.get('/hello', (req, res) => { res.send('Hello World!') }) but when consulting the /hello route it gives me 404 as a result, is there an error with the route handler? thank you

tawn33y commented 1 year ago

You need to supply the app value to the function for creating the server, ie:

await bot.startExpressServer({

  // add this:
  app, 

  webhookVerifyToken: process.env.VERIFY_TOKEN,
  port: process.env.PORT || 2040,
  webhookPath: '/webhook', 
});

Also, nit, perhaps you can move the async definition to the top, i.e:

(async function() { 
  const app = express();

  // ... 

  app.get('/hello', (req, res) => {
    res.send('Hello World!')
  })
})() 
KevinJoseph commented 1 year ago

Thank you very much, is it possible to integrate socket io too?

On Sun, Jan 1, 2023, 2:00 AM Tony @.***> wrote:

You need to supply the app value to the function for creating the server, ie:

await bot.startExpressServer({ // Add this: app, webhookVerifyToken: process.env.VERIFY_TOKEN,port: process.env.PORT || 2040,webhookPath: /webhook,});

Also, nit, perhaps you can move the async definition to the top, i.e:

(async function() { const app = express();// ... app.get('/hello', (req, res) => {res.send('Hello World!')})})()

— Reply to this email directly, view it on GitHub https://github.com/tawn33y/whatsapp-cloud-api/issues/47#issuecomment-1368368940, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD74MWGSYJF6VVLRQY7OQYTWQETR5ANCNFSM6AAAAAATN2QECI . You are receiving this because you authored the thread.Message ID: @.***>

KevinJoseph commented 1 year ago

Thank you very much, is it possible to integrate socket io too?

tawn33y commented 1 year ago

Most welcome.

Unsure why you would need socket.io since you won't be able to integrate it with the Whatsapp app. Unless if you mean integration with your own service (e.g. your own frontend), then yes, you can definitely do it.

KevinJoseph commented 1 year ago

That's right, I want to integrate it with my own interface

KevinJoseph commented 1 year ago

I am developing it this way but the app stops. const from = process.env.WHATSAPP_PHONE_NUMBER_ID; const token = process.env.WHATSAPP_TOKEN; const webhookVerifyToken = process.env.VERIFY_TOKEN; const bot = createBot(from, token);

(async function() { const app = express();

// ...

app.get('/hello', (req, res) => { res.send('Hello World!') })

await bot.startExpressServer({

// add this:
app, 

webhookVerifyToken: process.env.VERIFY_TOKEN,
port: process.env.PORT || 2040,
webhookPath: '/webhook', 

}); })()

If I comment these lines of the file, startExpressServer.js executes me correctly

tawn33y commented 1 year ago

Hi @KevinJoseph, Did you manage to fix this?

Can you clarify what you mean by If I comment these lines of the file; which lines specifically?

tawn33y commented 1 year ago

Closing this due to insufficient info. @KevinJoseph, feel free to reopen :)