danneu / telegram-chatgpt-bot

a Telegram ChatGPT bot that supports text prompts and two-way voice memos
35 stars 8 forks source link

streamTokensToTelegram fails due to undefined prevId #15

Closed damntourists closed 1 year ago

damntourists commented 1 year ago

Hi there!

I forked your repository the other week and have set up a couple of telegram bots running on kubernetes but noticed a couple of days ago that none of the bots can respond anymore due to the following error

makeTelegramRequest https://api.telegram.org/XXXXXX/sendChatAction
Error: Impossible: prevId remains undefined. (bug)
    at streamTokensToTelegram (/usr/src/app/index.ts:1219:15)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async completeAndSendAnswer (/usr/src/app/index.ts:600:52)
    at async processUserMessage (/usr/src/app/index.ts:515:20)
    at async handleWebhookUpdate (/usr/src/app/index.ts:1036:9)
makeTelegramRequest https://api.telegram.org/XXXXXX/sendMessage

I can take a stab at finding a fix for this unless you know of something off of the top of your head that will get them back up and running?

danneu commented 1 year ago

Hmm, I'm not sure off the top of my head nor can I reproduce it. Gotta love the hubris of "Impossible" though.

It looks like somehow the initial telegram.sendMessage() is succeeding (else it would throw an error before reaching your error) but the response body.id is somehow undefined.

https://github.com/danneu/telegram-chatgpt-bot/blob/1cb33eab190f00089b4513042f605865ce769ffe/index.ts#L1199-L1201

Fwiw, this code works by creating an initial prevId = telegram.sendMessage().then(m => m.id) message and then repeatedly using prevId to edit it as new tokens stream in from OpenAI's API.

I would wonder what the msg looks like in that code.

Maybe change this:

https://github.com/danneu/telegram-chatgpt-bot/blob/1cb33eab190f00089b4513042f605865ce769ffe/index.ts#L1199-L1201

to this:

const body = await telegram
                    .sendMessage(chatId, prefix + cursor, initMessageId)
console.log('sent telegram message.response body:', body)
prevId = body.message_id

Might show what's going on.

Sorry, I wish I had this issue so I could make it more robust, myself. My bot annoyingly is humming along just fine.

damntourists commented 1 year ago

Hey there, I wanted to let you know that I finally figured out what was wrong. My account had run out of credits, so I was no longer able to use the API key. It wasn't immediately obvious until I tried reusing the same key in a different app. I think we can close this!