Created
April 17, 2020 15:59
-
-
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
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
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