Skip to content

Instantly share code, notes, and snippets.

@deanshub
Last active January 4, 2019 13:24
Show Gist options
  • Save deanshub/ee1e432c40efcc7e150a479ceafafc78 to your computer and use it in GitHub Desktop.
Save deanshub/ee1e432c40efcc7e150a479ceafafc78 to your computer and use it in GitHub Desktop.
import config from 'config'
import TelegramBot from 'node-telegram-bot-api'
const options = {
polling: true,
}
const bot = new TelegramBot(config.BOT_TOKEN, options)
const allKeyboardOpts ={
reply_markup:JSON.stringify({
keyboard:[
['/start','/help'],
],
resize_keyboard: true,
one_time_keyboard: true,
}),
parse_mode: 'Markdown',
disable_web_page_preview:true,
}
bot.on('callback_query', (callbackQuery) => {
runCommand('callback', callbackQuery, callbackQuery.data)
})
export function sendMessage(id, message, extraOps){
return bot.sendMessage(id, message, {...allKeyboardOpts, ...extraOps})
}
let commands = {}
export function addCommand(command, fn){
commands[`${command.name}.${command.fn||'default'}`] = fn
if (command.regex){
bot.onText(command.regex, fn)
}
}
export function runCommand(command, msg, match, fnName='default'){
return commands[`${command}.${fnName}`].call(this, msg, match)
}
export function editMessageReplyMarkup(replyMarkup, options) {
return bot.editMessageReplyMarkup(replyMarkup, options)
}
export function editMessageText(text, options) {
return bot.editMessageText(text, options)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment