Skip to content

Instantly share code, notes, and snippets.

@ilamanov
Last active January 29, 2022 08:25
Show Gist options
  • Save ilamanov/05c9552ce98b0ccae81f83da20f51bdc to your computer and use it in GitHub Desktop.
Save ilamanov/05c9552ce98b0ccae81f83da20f51bdc to your computer and use it in GitHub Desktop.
Read Telegram message and extract info
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