Created
April 25, 2019 16:06
-
-
Save meleu/aa0ffbbaa2a0af7980dc54932befbaf8 to your computer and use it in GitHub Desktop.
learning telegram bot
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'); | |
const TOKEN = 'BLABLEBLIBLOBLU'; | |
const bot = new TelegramBot(TOKEN, {polling: true}); | |
const botoesSimNao = { | |
inline_keyboard: [ | |
[ | |
{text: 'Sim', callback_data: 'sim'}, | |
{text: 'Não', callback_data: 'nao'}, | |
] | |
] | |
}; | |
const botaoSim = { | |
inline_keyboard: [[ {text: ':white_check_mark:', callback_data: 'noop'} ]] | |
} | |
const botaoNao = { | |
inline_keyboard: [[ {text: ':no_entry:', callback_data: 'noop'} ]] | |
} | |
bot.on('callback_query', function onCallBackQuery(action) { | |
const data = action.data; | |
const msg = action.message; | |
const chatId = msg.chat.id; | |
switch(data) { | |
case 'sim': | |
bot.sendMessage(chatId, "Você escolheu sim!"); | |
msg.editMessageReplyMarkup(botaoSim); | |
break; | |
case 'nao': | |
bot.sendMessage(chatId, "Você escolheu nao!"); | |
msg.editMessageReplyMarkup(botaoNao); | |
break; | |
} | |
}); | |
function changeStatus(chatId, fromStatus, toStatus) { | |
bot.sendMessage( | |
chatId, | |
`Tem certeza que deseja mudar o status de "*${fromStatus}*" para "*${toStatus}*"?`, | |
{ | |
parse_mode: "Markdown", | |
reply_markup: botoesSimNao | |
}, | |
); | |
} | |
bot.onText(/^\/ping/, msg => bot.sendMessage(msg.chat.id, 'pong!')); | |
bot.onText(/^\/(start|iniciar)/, msg => changeStatus(msg.chat.id, 'finalizado', 'em carregamento')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment