From 307ed01e4b03d25d003fd47cd02dd7a7b55564e0 Mon Sep 17 00:00:00 2001 From: NeverBehave Date: Wed, 23 Sep 2026 00:29:11 -0700 Subject: [PATCH] Restore private message replies --- index.js | 39 +++++++++++++++++++++------------------ test/index.test.js | 8 ++++++-- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index a3bb958..b789dc2 100644 --- a/index.js +++ b/index.js @@ -98,6 +98,26 @@ async function callTelegram(env, method, payload, fetcher) { return result.result } +export async function handleTextMessage(message, env, fetcher = fetch) { + if (!message || typeof message.text !== 'string' || message.chat?.id == null) { + return null + } + + const replies = isStartCommand(message.text, env.BOT_USERNAME) + ? [START_MESSAGE] + : splitIntoMessages(message.text) + const results = [] + + for (const text of replies) { + results.push(await callTelegram(env, 'sendMessage', { + chat_id: message.chat.id, + text, + }, fetcher)) + } + + return results.length === 1 ? results[0] : results +} + export async function handleUpdate(update, env, fetcher = fetch) { if (update.inline_query) { const query = update.inline_query.query || '114514' @@ -121,24 +141,7 @@ export async function handleUpdate(update, env, fetcher = fetch) { }, fetcher) } - const message = update.message - if (!message || typeof message.text !== 'string') { - return null - } - - const replies = isStartCommand(message.text, env.BOT_USERNAME) - ? [START_MESSAGE] - : splitIntoMessages(message.text) - const results = [] - - for (const text of replies) { - results.push(await callTelegram(env, 'sendMessage', { - chat_id: message.chat.id, - text, - }, fetcher)) - } - - return results.length === 1 ? results[0] : results + return handleTextMessage(update.message, env, fetcher) } export async function handleRequest(request, env, fetcher = fetch) { diff --git a/test/index.test.js b/test/index.test.js index cd225b9..741fb3f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -53,11 +53,15 @@ test('start commands addressed to another bot are ignored', () => { assert.equal(isStartCommand('/start@kongebot'), false) }) -test('text messages are sent back with spaces', async () => { +test('private text messages are sent back to the same chat with spaces', async () => { const mock = telegramMock() await handleUpdate({ - message: { chat: { id: 42 }, text: '你好' }, + message: { + message_id: 123, + chat: { id: 42, type: 'private' }, + text: '你好', + }, }, { BOT_TOKEN: 'test-token' }, mock.fetcher) assert.equal(mock.calls.length, 1)