skip to Main Content

I created a telegram bot in node.js. It worked fine in localhost. But when I tried to deploy it in render, It shows build successful and on starting service I am getting an error: ETELEGRAM: 409 Conflict: terminated by other getUpdates request; make sure that only one bot instance is running"}. I don’t have any other instance running.

I Was trying to deploy my telegram bot in render. Here’s my code:

 import TelegramBot from "node-telegram-bot-api";
    import { Configuration, OpenAIApi } from "openai";
    import { config } from "dotenv";


    config()

    const TOKEN = process.env.TELEGRAM_TOKEN

    const bot = new TelegramBot(TOKEN, {polling:true} )
    let firstMsg = true;

    bot.on('message', (message)=>{
        if (firstMsg) {
            bot.sendMessage(message.chat.id, `Hello ${message.chat.first_name}, use "/prompt" followed by your query`)
            firstMsg = false
        }
    })


    bot.onText(//prompt (.+)/, (msg, match) => {
        const chatId = msg.chat.id
        const messageText = match[1]

        

        openai.createChatCompletion({
            model:"gpt-3.5-turbo",
            messages:[{role:"user", content:messageText}]
        }).then(res=>{
            const result = (res.data.choices[0].message.content) 
            bot.sendMessage(chatId, result);
        })
        

      });
      
    const openai = new OpenAIApi(new Configuration({
        apiKey:process.env.CHATGPT_API
    }))

2

Answers


  1. You need to double check if bot token that you are using is correct and you are not using the same bot token locally.

    I’d suggest to create another one bot just for running it locally for testing and development and use production bot for deployed application.

    Login or Signup to reply.
  2. For the production version, this is not correct. You need to install webhook. You can find it on the site after deployment and install it in env. Try something like this for yourself.

    const cb = function(req, res) {
        res.end(`${bot.options.username}`)
    }
    
    try {
        if(process.env.PROD) {
            bot.launch({
                webhook: {
                    domain: `${process.env.URL}`,
                    port: `${process.env.PORT}`,
                    cb
                }
            })
        } else {
            bot.launch()
        }
    } catch(e) {
        console.log('ERROR: ' + e)
    }
    

    Here used a telegraf lib

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search