From 66eb144aeb7325798caf73cfc5ea108b651c35d8 Mon Sep 17 00:00:00 2001 From: printfn Date: Thu, 28 Dec 2023 01:39:15 +0000 Subject: [PATCH] Fix async function in telegram bot --- telegram-bot/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/telegram-bot/index.js b/telegram-bot/index.js index c34d54a2..e75980b3 100644 --- a/telegram-bot/index.js +++ b/telegram-bot/index.js @@ -11,7 +11,7 @@ LAMBDA_URL="..." curl "https://api.telegram.org/bot${TELEGRAM_BOT_API_TOKEN}/setWebhook" --form-string "url=${LAMBDA_URL}" */ -async function processInput(input, chatType) { +function processInput(input, chatType) { if (input == '/start' || input == '/help') { return "fend is an arbitrary-precision unit-aware calculator.\n\nYou can send it maths questions like '1+1', 'sin(pi)' or 'sqrt(5)'. In group chats, you'll need to enclose your input in [[double square brackets]] like this: [[1+1]]."; } @@ -39,11 +39,11 @@ async function processMessage(message) { let text = message.text; let result = processInput(text, message.chat.type); if (result != null && result != '') { - await postJSON(`https://api.telegram.org/bot${TELEGRAM_BOT_API_TOKEN}/sendChatAction`, { + await postJSON('sendChatAction', { chat_id: message.chat.id, action: 'typing', }); - await postJSON(`https://api.telegram.org/bot${TELEGRAM_BOT_API_TOKEN}/sendMessage`, { + await postJSON('sendMessage', { chat_id: message.chat.id, text: result, disable_web_page_preview: true, @@ -65,7 +65,7 @@ async function processUpdate(update) { if (result != null && result != '') { results.push({type: 'article', title: result, id: '1', input_message_content: {message_text: result}}); } - await postJSON(`https://api.telegram.org/bot${TELEGRAM_BOT_API_TOKEN}/answerInlineQuery`, { + await postJSON('answerInlineQuery', { inline_query_id: update.inline_query.id, results, }); @@ -77,7 +77,7 @@ async function pollUpdates() { var highestOffet = 441392434; while (true) { console.log('Polling getUpdates (30s)...') - let updates = await postJSON(`https://api.telegram.org/bot${TELEGRAM_BOT_API_TOKEN}/getUpdates`, { + let updates = await postJSON('getUpdates', { timeout: 30, offset: highestOffet + 1, }); @@ -91,8 +91,8 @@ async function pollUpdates() { } }; -async function postJSON(url, body) { - let response = await fetch(url, { +async function postJSON(endpoint, body) { + let response = await fetch(`https://api.telegram.org/bot${TELEGRAM_BOT_API_TOKEN}/${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json ; charset=UTF-8',