Skip to content

Instantly share code, notes, and snippets.

@Fast0n
Last active May 21, 2018 15:50
Show Gist options
  • Save Fast0n/fb525e6f8c84f91a1d7e7a6e7b60c71c to your computer and use it in GitHub Desktop.
Save Fast0n/fb525e6f8c84f91a1d7e7a6e7b60c71c to your computer and use it in GitHub Desktop.
Multiple callback query handlers telepot
from settings import token, start_msg, client_file
from telepot.loop import MessageLoop
from telepot.namedtuple import ReplyKeyboardMarkup, ReplyKeyboardRemove, InlineKeyboardMarkup, InlineKeyboardButton
from time import sleep
import os
import sys
import telepot
def on_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[dict(text='Visualizza', callback_data=1)]
])
bot.sendMessage(chat_id, 'Capitolo 1',
reply_markup=keyboard)
def on_callback_query(msg):
query_id, from_id, query_data = telepot.glance(
msg, flavor='callback_query')
edited = (from_id, msg['message']['message_id'])
if (query_data == str(1)):
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[dict(text='Avanti', callback_data=2)]
])
bot.editMessageText(edited, 'Paragrafo 1',
reply_markup=keyboard)
if (query_data == str(2)):
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[dict(text='Indietro', callback_data=1),
dict(text='Avanti', callback_data=3)]
])
bot.editMessageText(edited, 'Paragrafo 2',
reply_markup=keyboard)
if (query_data == str(3)):
keyboard = InlineKeyboardMarkup(inline_keyboard=[
[dict(text='Indietro', callback_data=2),
dict(text='Fine', callback_data=4)]
])
bot.editMessageText(edited, 'Paragrafo 3',
reply_markup=keyboard)
if (query_data == str(4)):
bot.editMessageText(edited, 'Fine')
# Main
print("Avvio FindEAT_Bot")
# PID file
pid = str(os.getpid())
pidfile = "/tmp/FindEAT_Bot.pid"
# Check if PID exist
if os.path.isfile(pidfile):
print("%s already exists, exiting!" % pidfile)
sys.exit()
# Create PID file
f = open(pidfile, 'w')
f.write(pid)
# Start working
try:
bot = telepot.Bot(token)
MessageLoop(bot, {'chat': on_chat_message,
'callback_query': on_callback_query}).run_as_thread()
while(1):
sleep(10)
finally:
os.unlink(pidfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment