Skip to content

Commit

Permalink
Fix async function in telegram bot
Browse files Browse the repository at this point in the history
  • Loading branch information
printfn committed Dec 28, 2023
1 parent 4bff6fb commit 66eb144
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions telegram-bot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]].";
}
Expand Down Expand Up @@ -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,
Expand All @@ -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,
});
Expand All @@ -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,
});
Expand All @@ -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',
Expand Down

0 comments on commit 66eb144

Please sign in to comment.