all repos — kokyo @ 6236a1282014a4097ecb54d5a9e9ed6bd7ae796d

Chatbot and CLI tool for Swiss public transports

🔊 Improve loggin
Tim Izzo tim@5ika.ch
Sun, 18 Aug 2024 10:59:20 +0200
commit

6236a1282014a4097ecb54d5a9e9ed6bd7ae796d

parent

9547bc3cdaf7c4d51c589d191c664af4c34bcfdf

2 files changed, 36 insertions(+), 21 deletions(-)

jump to
A bots/telegram.d.ts

@@ -0,0 +1,8 @@

+interface From { + id: string; + first_name?: string; + last_name?: string; + username?: string; + is_bot: boolean; + language_code: string; +}
M bots/telegram.tsbots/telegram.ts

@@ -15,13 +15,19 @@ .replaceAll("(", "\\(")

.replaceAll(")", "\\)"); const sendMessage = async (message: any) => { - const response = await telegram.api.sendMessage(message); - console.log(response); + await telegram.api.sendMessage(message); +}; + +const getUsername = (from: From) => { + let username = `id:${from.id}`; + if (from.username) username = `user:${from.username}`; + if (from.first_name && from.last_name) + return `${from.first_name} ${from.last_name} (${username})`; + else return username; }; for await (const update of getUpdates(telegram)) { - console.log(update); - + logger.debug(JSON.stringify(update, null, 4)); // On new message if (update.message) { if (!update.message?.from) {

@@ -29,9 +35,9 @@ console.error("No 'from' in message");

continue; } - logger.info( - `New message from ${update.message.from.username}: ${update.message.text}` - ); + const { from, text } = update.message; + + logger.info(`New message from ${getUsername(from)}: ${text}`); telegram.api.setMyCommands({ commands: [

@@ -43,9 +49,9 @@ },

], }); - if (update.message.text === "/aide" || update.message.text === "/start") { + if (text === "/aide" || text === "/start") { sendMessage({ - chat_id: update.message.from.id, + chat_id: from.id, text: `Hey ! Je suis un bot qui te permet d'obtenir rapidement des informations sur les transports en commun dans toute la Suisse. Entre le nom d'un arrĂȘt et laisse-toi guider !

@@ -54,8 +60,8 @@

/aide - Affiche ce message /favoris - Affiche vos favoris`, }); - } else if (update.message.text === "/favoris") { - const userId = `telegram:${update.message.from.id}`; + } else if (text === "/favoris") { + const userId = `telegram:${from.id}`; const favorites = kvdb.getUserFavorites(userId); let inlineKeyboard = new InlineKeyboard();

@@ -68,12 +74,12 @@ })

.row(); await sendMessage({ - chat_id: update.message.from.id, + chat_id: from.id, text: "Voici vos favoris", reply_markup: inlineKeyboard, }); } else { - const stopLists = await findStopByName(update.message.text); + const stopLists = await findStopByName(text); kvdb.saveStops(stopLists); let keyboard = new InlineKeyboard(); for (const stop of stopLists)

@@ -82,7 +88,7 @@ .text(stop.name, { cmd: "nextDepartures", stopRef: stop.stopRef })

.row(); await sendMessage({ - chat_id: update.message.from.id, + chat_id: from.id, text: "Quel arrĂȘt correspond ?", reply_markup: keyboard, });

@@ -91,12 +97,11 @@ }

// On keyboard event (callback query) else if (update.callback_query) { - const userId = `telegram:${update.callback_query.from.id}`; - const payload = JSON.parse(update.callback_query.data); + const { from, data, message } = update.callback_query; + const userId = `telegram:${from.id}`; + const payload = JSON.parse(data); - logger.info( - `New request from ${update.callback_query.from.username}: ${update.callback_query.data}` - ); + logger.info(`New request from ${getUsername(from)}: ${data}`); if (payload?.cmd === "nextDepartures") { const nextDepartures = await getNextDepartures(payload.stopRef);

@@ -114,6 +119,7 @@

let keyboard = new InlineKeyboard().text("RafraĂźchir", { cmd: "nextDepartures", stopRef: payload.stopRef, + refresh: message.message_id, }); const existingFavorite = await kvdb.getUserFavorite( userId,

@@ -124,8 +130,9 @@ keyboard = keyboard.text("Enregistrer comme favoris", {

cmd: "saveFavorite", stopRef: payload.stopRef, }); + await sendMessage({ - chat_id: update.callback_query.from.id, + chat_id: from.id, parse_mode: "MarkdownV2", text, reply_markup: keyboard,

@@ -134,7 +141,7 @@ } else if (payload?.cmd === "saveFavorite") {

const stop = await kvdb.getStopByRef(payload.stopRef); if (stop?.value) await kvdb.saveUserFavorite(userId, stop.value); await sendMessage({ - chat_id: update.callback_query.from.id, + chat_id: from.id, parse_mode: "MarkdownV2", text: `L'arrĂȘt *${formatContent( stop.value?.name