Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

possible error when adding routes #47

Closed
KevinJoseph opened this issue Jan 1, 2023 · 8 comments
Closed

possible error when adding routes #47

KevinJoseph opened this issue Jan 1, 2023 · 8 comments
Labels
question Further information is requested

Comments

@KevinJoseph
Copy link

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
Copy link
Owner

tawn33y commented Jan 1, 2023

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
Copy link
Author

KevinJoseph commented Jan 1, 2023 via email

@KevinJoseph
Copy link
Author

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

@tawn33y
Copy link
Owner

tawn33y commented Jan 1, 2023

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
Copy link
Author

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

@KevinJoseph
Copy link
Author

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 tawn33y added the question Further information is requested label Jan 11, 2023
@tawn33y
Copy link
Owner

tawn33y commented Feb 8, 2023

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
Copy link
Owner

tawn33y commented Mar 10, 2023

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

@tawn33y tawn33y closed this as not planned Won't fix, can't repro, duplicate, stale Mar 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
Status: Closed
Development

No branches or pull requests

2 participants