Skip to content

Instantly share code, notes, and snippets.

@khaschuluu
Created October 2, 2023 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khaschuluu/68798cdb4d25180c66e880b0de4d6f71 to your computer and use it in GitHub Desktop.
Save khaschuluu/68798cdb4d25180c66e880b0de4d6f71 to your computer and use it in GitHub Desktop.
Telegram bot exmaple
// This is a Telegram Bot that get's message command from group that have this bot. For this example:
// When someone send message comand like `/lunch`, then it reply random restaruant name etc.
const functions = require('@google-cloud/functions-framework');
var TelegramBot = require('telegrambot');
var api = new TelegramBot('9999999999:AAAA_bbbb-cccDD-EEEEEEEEEE-9999-FFF');
const restaurants = [
"Samgyopsal",
"Dino",
"Arig",
"La viva",
"Gurvan gal",
"Buuz",
"Uulzbar",
"Stockholm",
"Jade",
"Grand",
"Business plaza",
"Limo",
"Ayanchin",
"Chopsticks",
"Burger King",
"Fat Hen"
];
const bars = [
"Samgyopsal",
"Gurvan gal",
"Uulzbar",
"Stockholm",
"Jade",
"Chopsticks",
"Fat Hen",
"Brooklyn",
"MB"
];
functions.http('randrest', async (req, res) => {
let message = "";
console.log(JSON.stringify(req.body));
if (req.body?.message?.chat?.id === -746615751 && req.body?.message?.text?.match(/^\/lunch/g)) {
message = `Сайн уу, @${req.body.message.from.username}? Өдрийг сайхан өнгөрүүлж байна уу?
Өнөөдөр бөөнөөрөө ${restaurants[Math.floor(Math.random()*restaurants.length)]} орылдоо 😇`;
} else if (req.body?.message?.chat?.id === -746615751 && req.body?.message?.text?.match(/^\/guntseg/g)) {
message = `Эёоу, @${req.body.message.from.username}, шөнийг яажшуухан өнгөрөөх гэжийндаа?
Өнөөдөр бөөнөөрөө ${bars[Math.floor(Math.random()*bars.length)]} орж бут авылдаа 😈`;
} else if (req.body.message.text.match(/^\/ping/g)) {
message = "pong"
} else if (req.body.message.text.match(/^\/ching/g)) {
message = "chong"
} else if (req.body.message.text.match(/^\/marko/g)) {
message = "polo"
} else if (req.body.message.text.match(/^\/jimijimi/g)) {
message = "haja haja"
}
if (message) {
await api.sendMessage({
chat_id: "-746615751",
text: message,
reply_to_message_id: req.body.message.message_id
}, (err, message) => {
if (err) throw err;
console.log(message);
});
}
res.send("ok");
});
@khaschuluu
Copy link
Author

This works on Google Cloud Functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment