Last active
January 29, 2022 08:25
-
-
Save ilamanov/05c9552ce98b0ccae81f83da20f51bdc to your computer and use it in GitHub Desktop.
Read Telegram message and extract info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const TELEGRAM_TOKEN = "..." | |
| export default async (req, res) => { | |
| const chat_id = req.body.message.chat.id; | |
| try { | |
| const sentMessage = req.body.message.text; | |
| const splitInHalf = sentMessage.split("Remind in") | |
| const firstPart = splitInHalf[0].trim() | |
| const secondPart = splitInHalf[1].trim() | |
| const reminder = firstPart | |
| const splitInHalfAgain = secondPart.split("minutes") | |
| const minutes = splitInHalfAgain[0].trim() | |
| let responseText = `Done, will remind in ${minutes} minutes!` | |
| await respond(chat_id, responseText) | |
| } catch (e) { | |
| await respond(chat_id, "unexpected error") | |
| } | |
| res.status(200).send({}); | |
| } | |
| async function respond(chat_id, responseText) { | |
| await fetch( | |
| `https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendMessage`, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| }, | |
| body: JSON.stringify({ | |
| chat_id: chat_id, | |
| text: responseText, | |
| }) | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment