Created
October 2, 2023 07:52
-
-
Save khaschuluu/68798cdb4d25180c66e880b0de4d6f71 to your computer and use it in GitHub Desktop.
Telegram bot exmaple
This file contains 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
// 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"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works on Google Cloud Functions