我在 node.js 中创建了一个电报机器人。它在本地主机上运行良好。但是当我尝试在渲染中部署它时,它显示构建成功并且在启动服务时出现错误:ETELEGRAM: 409 Conflict: terminated by other getUpdates request;确保只有一个机器人实例在运行”}。我没有任何其他实例在运行。
我试图在渲染中部署我的电报机器人。这是我的代码:
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
}))
您需要仔细检查您使用的机器人令牌是否正确,并且您没有在本地使用相同的机器人令牌。
我建议创建另一个机器人,仅用于在本地运行它以进行测试和开发,并使用生产机器人部署应用程序。
对于生产版本,这是不正确的。您需要安装网络钩子。部署后可以在站点上找到并安装到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)
}
这里使用了一个电报库