I am trying to make a telegram bot with node js. What my bot do is making phone calls based on what the telegram user input. My bot works fine at the first execution but when I try to do multiple task it get stuck in a weird loop an bassicaly send the message one more time each time I give input.
Its like the code never break the switch and always listen for a number input.
Here is my code in node:
require("dotenv").config();
const axios = require("axios");
const express = require("express");
const port = 5000;
const app = express();
const Voice = require("@signalwire/realtime-api").Voice;
const { TOKEN, SERVER_URL } = process.env;
const URI = `/webhook/${TOKEN}`;
const TelegramBot = require("node-telegram-bot-api");
const { reset, restart } = require("nodemon");
const nodemon = require("nodemon");
var chatId;
// replace the value below with the Telegram token you receive from @BotFather
const token = "<token>";
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, { polling: true });
bot.on("message", (msg) => {
chatId = msg.chat.id;
if (msg.text == "/call") {
chooseBank();
}
if (msg.text == "/restart") {
recover();
}
});
//GIVE OPTION FOR THE CLIENT
function chooseBank() {
bot.sendMessage(
chatId,
"CHOOSE THE BANK: nn1 RBCn2 CIBCn3 SCOTIAn4 BMOn5 NATIONALn6 TDn7 PC-FINANCIALn8 DESJARDINS"
);
bot.on("message", (msg) => {
switch (msg.text) {
case "1":
bot.sendMessage(
chatId,
"ENTER THE CLIENTS NUMBER:nnEX: (+15142220000)"
);
break;
case "2":
bot.sendMessage(
chatId,
"ENTER THE CLIENTS NUMBER:nnEX: (+15142220000)"
);
break;
case "3":
bot.sendMessage(
chatId,
"ENTER THE CLIENTS NUMBER:nnEX: (+15142220000)"
);
break;
case "4":
bot.sendMessage(
chatId,
"ENTER THE CLIENTS NUMBER:nnEX: (+15142220000)"
);
break;
case "5":
bot.sendMessage(
chatId,
"ENTER THE CLIENTS NUMBER:nnEX: (+15142220000)"
);
break;
case "6":
bot.sendMessage(
chatId,
"ENTER THE CLIENTS NUMBER:nnEX: (+15142220000)"
);
break;
case "7":
bot.sendMessage(
chatId,
"ENTER THE CLIENTS NUMBER:nnEX: (+15142220000)"
);
break;
case "8":
bot.sendMessage(
chatId,
"ENTER THE CLIENTS NUMBER:nnEX: (+15142220000)"
);
}
});
}
function recover(){
chooseBank()
}
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
your text`
Here Are the example of running my bot in telegram: