NodeJS Telegram bot错误,409冲突:由其他getUpdates请求终止

0qx6xfy6  于 2023-05-17  发布在  Node.js
关注(0)|答案(2)|浏览(303)

我在node.js中创建了一个电报机器人。在localhost上运行良好。但是当我试图在渲染中部署它时,它显示构建成功,并且在启动服务时我得到一个错误:ETELEGRAM:409由其他getUpdates请求终止;确保只有一个bot示例正在运行”}。我没有运行任何其他示例。
我想在渲染中部署我的电报机器人。下面是我的代码:

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
    }))
7qhs6swi

7qhs6swi1#

您需要仔细检查您使用的机器人令牌是否正确,并且您在本地没有使用相同的机器人令牌。
我建议创建另一个机器人只是为了在本地运行它进行测试和开发,并使用生产机器人部署应用程序。

e0bqpujr

e0bqpujr2#

对于生产版本,这是不正确的。你需要安装webhook。您可以在部署后在站点上找到它并将其安装在env中。你自己试试看。

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)
}

这里使用了一个telegraf库

相关问题