Skip to content

Instantly share code, notes, and snippets.

@Mahdhir
Created April 17, 2020 15:59
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 Mahdhir/e3c08058d03cbd6e4419c7bd54cd9664 to your computer and use it in GitHub Desktop.
Save Mahdhir/e3c08058d03cbd6e4419c7bd54cd9664 to your computer and use it in GitHub Desktop.
A code snippet to listen and send messages, photos to your telegram bot implemented in Node JS
const TelegramBot = require('node-telegram-bot-api');
// replace the value below with the Telegram token you receive from @BotFather
const token = 'YOUR_TELEGRAM_BOT_TOKEN';
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, {polling: true});
// Listen for any kind of message. There are different kinds of
// messages.
bot.on('message', (msg) => {
const chatId = msg.chat.id;
console.log(msg);
// send a message to the chat acknowledging receipt of their message
const text = msg.text;
if(text.includes('picture') || text.includes('image')){
const imgUrl = "https://picsum.photos/500/700";
bot.sendPhoto(chatId,imgUrl);
}else{
bot.sendMessage(chatId, 'Received your message:'+text);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment