2016-04-16 20:32:44 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Basic example for a bot that awaits an answer from the user. It's built upon
|
|
|
|
# the state_machine_bot.py example
|
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from telegram import Emoji, ForceReply, InlineKeyboardButton, \
|
|
|
|
InlineKeyboardMarkup
|
|
|
|
from telegram.ext import Updater, CommandHandler, MessageHandler, \
|
2016-04-25 13:26:52 +02:00
|
|
|
CallbackQueryHandler, Filters
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - '
|
|
|
|
'%(message)s',
|
|
|
|
level=logging.DEBUG)
|
|
|
|
|
|
|
|
# Define the different states a chat can be in
|
|
|
|
MENU, AWAIT_CONFIRMATION, AWAIT_INPUT = range(3)
|
|
|
|
|
|
|
|
# Python 2 and 3 unicode differences
|
|
|
|
try:
|
|
|
|
YES, NO = (Emoji.THUMBS_UP_SIGN.decode('utf-8'),
|
|
|
|
Emoji.THUMBS_DOWN_SIGN.decode('utf-8'))
|
|
|
|
except AttributeError:
|
|
|
|
YES, NO = (Emoji.THUMBS_UP_SIGN, Emoji.THUMBS_DOWN_SIGN)
|
|
|
|
|
|
|
|
# States are saved in a dict that maps chat_id -> state
|
|
|
|
state = dict()
|
|
|
|
# Sometimes you need to save data temporarily
|
|
|
|
context = dict()
|
|
|
|
# This dict is used to store the settings value for the chat.
|
|
|
|
# Usually, you'd use persistence for this (e.g. sqlite).
|
|
|
|
values = dict()
|
|
|
|
|
|
|
|
|
|
|
|
# Example handler. Will be called on the /set command and on regular messages
|
|
|
|
def set_value(bot, update):
|
|
|
|
chat_id = update.message.chat_id
|
|
|
|
user_id = update.message.from_user.id
|
|
|
|
user_state = state.get(chat_id, MENU)
|
|
|
|
|
|
|
|
if user_state == MENU:
|
|
|
|
state[user_id] = AWAIT_INPUT # set the state
|
|
|
|
bot.sendMessage(chat_id,
|
2016-04-16 21:11:41 +02:00
|
|
|
text="Please enter your settings value",
|
2016-04-16 20:32:44 +02:00
|
|
|
reply_markup=ForceReply())
|
|
|
|
|
|
|
|
|
|
|
|
def entered_value(bot, update):
|
|
|
|
chat_id = update.message.chat_id
|
|
|
|
user_id = update.message.from_user.id
|
|
|
|
chat_state = state.get(user_id, MENU)
|
|
|
|
|
|
|
|
# Check if we are waiting for input
|
|
|
|
if chat_state == AWAIT_INPUT:
|
|
|
|
state[user_id] = AWAIT_CONFIRMATION
|
|
|
|
|
|
|
|
# Save the user id and the answer to context
|
|
|
|
context[user_id] = update.message.text
|
|
|
|
reply_markup = InlineKeyboardMarkup(
|
|
|
|
[[InlineKeyboardButton(YES, callback_data=YES),
|
|
|
|
InlineKeyboardButton(NO, callback_data=NO)]])
|
|
|
|
bot.sendMessage(chat_id, text="Are you sure?",
|
|
|
|
reply_markup=reply_markup)
|
|
|
|
|
|
|
|
|
|
|
|
def confirm_value(bot, update):
|
2016-04-16 20:55:43 +02:00
|
|
|
query = update.callback_query
|
|
|
|
chat_id = query.message.chat_id
|
|
|
|
user_id = query.from_user.id
|
|
|
|
text = query.data
|
2016-04-16 20:32:44 +02:00
|
|
|
user_state = state.get(user_id, MENU)
|
|
|
|
user_context = context.get(user_id, None)
|
|
|
|
|
|
|
|
# Check if we are waiting for confirmation and the right user answered
|
|
|
|
if user_state == AWAIT_CONFIRMATION:
|
|
|
|
del state[user_id]
|
|
|
|
del context[user_id]
|
2016-04-16 20:55:43 +02:00
|
|
|
bot.answerCallbackQuery(query.id, text="Ok!")
|
2016-04-16 20:32:44 +02:00
|
|
|
if text == YES:
|
|
|
|
values[user_id] = user_context
|
|
|
|
bot.editMessageText(text="Changed value to %s." % values[user_id],
|
|
|
|
chat_id=chat_id,
|
|
|
|
message_id=
|
2016-04-16 20:55:43 +02:00
|
|
|
query.message.message_id)
|
2016-04-16 20:32:44 +02:00
|
|
|
else:
|
|
|
|
bot.editMessageText(text="Alright, value is still %s."
|
2016-04-16 21:03:30 +02:00
|
|
|
% values.get(user_id, 'not set'),
|
2016-04-16 20:32:44 +02:00
|
|
|
chat_id=chat_id,
|
|
|
|
message_id=
|
2016-04-16 20:55:43 +02:00
|
|
|
query.message.message_id)
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def help(bot, update):
|
|
|
|
bot.sendMessage(update.message.chat_id, text="Use /set to test this bot.")
|
|
|
|
|
|
|
|
|
|
|
|
def error(bot, update, error):
|
2016-04-16 20:55:43 +02:00
|
|
|
logging.warning('Update "%s" caused error "%s"' % (update, error))
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
# Create the Updater and pass it your bot's token.
|
2016-04-22 09:11:52 +02:00
|
|
|
updater = Updater("TOKEN")
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
# The command
|
|
|
|
updater.dispatcher.addHandler(CommandHandler('set', set_value))
|
|
|
|
# The answer
|
2016-04-28 17:29:34 +02:00
|
|
|
updater.dispatcher.add_handler(MessageHandler([Filters.text], entered_value))
|
2016-04-16 20:32:44 +02:00
|
|
|
# The confirmation
|
2016-04-28 17:29:34 +02:00
|
|
|
updater.dispatcher.add_handler(CallbackQueryHandler(confirm_value))
|
|
|
|
updater.dispatcher.add_handler(CommandHandler('start', help))
|
|
|
|
updater.dispatcher.add_handler(CommandHandler('help', help))
|
|
|
|
updater.dispatcher.add_error_handler(error)
|
2016-04-16 20:32:44 +02:00
|
|
|
|
|
|
|
# Start the Bot
|
|
|
|
updater.start_polling()
|
|
|
|
|
|
|
|
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
|
|
|
|
# SIGTERM or SIGABRT
|
|
|
|
updater.idle()
|