Skip to content

Instantly share code, notes, and snippets.

@alikhil
Created February 12, 2020 11:28
Show Gist options
  • Save alikhil/0edbad0d3d867dd9ed860b9916ccd06a to your computer and use it in GitHub Desktop.
Save alikhil/0edbad0d3d867dd9ed860b9916ccd06a to your computer and use it in GitHub Desktop.
import logging
from os import getenv
from dotenv import load_dotenv
load_dotenv()
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton
from telegram.ext import ConversationHandler, CallbackContext, CommandHandler, MessageHandler, Filters, Updater, CallbackQueryHandler
from telegram.ext.dispatcher import run_async
TOKEN = 'token'
proxy = {}
# proxy = {
# 'proxy_url': getenv('TELEGRAM_PROXY_HOST'),
# 'urllib3_proxy_kwargs': {
# 'username': getenv('TELEGRAM_PROXY_USERNAME'),
# 'password': getenv('TELEGRAM_PROXY_PASSWORD'),
# }
# }
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
WAIT_FOR_TEXT_ANSWER=1
@run_async
def echo(update, context):
"""Echo the user message."""
markup = InlineKeyboardMarkup(
[
[InlineKeyboardButton('Test button', callback_data='data')],
]
)
update.message.reply_text(update.message.text, reply_markup=markup)
@run_async
def answer_feedback(update: Update, context: CallbackContext):
update.callback_query.answer(text='')
context.bot.send_message(update.callback_query.message.chat.id, 'write me back')
return WAIT_FOR_TEXT_ANSWER
@run_async
def put_answer(update: Update, context):
logger.info('put_answer')
update.message.reply_text('get your text')
# update.(update.message.text)
return ConversationHandler.END
@run_async
def cancel(update: Update, context):
'Finishing any conversation.'
update.message.reply_text('ok, ending conversation')
return ConversationHandler.END
def run():
updater = Updater(TOKEN,
use_context=True,
request_kwargs=proxy,
workers=32)
updater.dispatcher.add_handler(MessageHandler(Filters.text, echo))
feedback_conv = ConversationHandler(
per_message=True,
# per_chat=True,
entry_points=[CallbackQueryHandler(answer_feedback, pattern='^data$')],
states={
WAIT_FOR_TEXT_ANSWER: [
MessageHandler(Filters.text, put_answer),
]
},
fallbacks=[
CommandHandler('cancel', cancel),
]
)
updater.dispatcher.add_handler(feedback_conv)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment