You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
7.9 KiB
7.9 KiB
In [1]:
import logging from telegram import ReplyKeyboardMarkup, Update from telegram.ext import ( Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, PicklePersistence, CallbackContext, )
In [2]:
# Enable logging logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO ) logger = logging.getLogger(__name__) CHOOSING, TYPING_REPLY, TYPING_CHOICE = range(3) #Create bot keyboard reply_keyboard = [ ['I would Submit a Translation'], ['Want an Explaination', 'Go to the Project'], ['Done'], ] markup = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
In [3]:
def facts_to_str(user_data): facts = list() for key, value in user_data.items(): facts.append('{}'.format(value)) return "\n".join(facts).join(['\n', '\n'])
In [4]:
# the bot starts here def start(update: Update, context: CallbackContext) -> None: first_name = update.message.from_user['first_name'] reply_text = f"Hi, {first_name}! This is blabla pratical bot" update.message.reply_text(reply_text, reply_markup=markup) return CHOOSING
In [5]:
#function for submitting def submit(update: Update, context: CallbackContext) -> None: text = update.message.text.lower() context.user_data['choice'] = text #if context.user_data.get(text): # reply_text = 'Your {}, I already know the following ' 'about that: {}'.format( # text, context.user_data[text] # ) #else: reply_text = f'Super! Please, respect the following format: \n\n english : translation : which-language \n\n For Example: \n hello : ciao : italian'.format(text) update.message.reply_text(reply_text) return TYPING_REPLY
In [6]:
#function for explaination def exp(update: Update, context: CallbackContext) -> None: first_name = update.message.from_user['first_name'] update.message.reply_text(f"This is an explaination, {first_name}", reply_markup= markup)
In [7]:
#function for submitting def project(update: Update, context: CallbackContext) -> None: update.message.reply_text(f"Here it is! \n \n https://issue.xpub.nl/ \n\n Made with love from XPUB1", reply_markup= markup)
In [8]:
#function for echo submission def received_information(update: Update, context: CallbackContext) -> None: first_name = update.message.from_user['first_name'] text = update.message.text category = context.user_data['choice'] context.user_data[category] = text.lower() del context.user_data['choice'] update.message.reply_text(f"Cool {first_name}!" "You have just submitted" "{}" "You can submit more translations, if you want :)".format(facts_to_str(context.user_data)), reply_markup=markup, ) return CHOOSING
In [9]:
#function for echo everything submitted def show_data(update: Update, context: CallbackContext) -> None: update.message.reply_text( "This is what you already told me:" "{}".format(facts_to_str(context.user_data)) )
In [10]:
#function for end bot and echo everything submitted def done(update: Update, context: CallbackContext) -> None: update.message.reply_text( "This is what you submitted today:" "{}".format(facts_to_str(context.user_data)) ) return ConversationHandler.END
In [ ]:
def main(): # Create the Updater and pass it your bot's token. pp = PicklePersistence(filename='conversationbot') updater = Updater("1326953998:AAGJHedo9kXOfN1CRJisygAhYMmxOwGUSCQ", persistence=pp, use_context=True) # Get the dispatcher to register handlers dp = updater.dispatcher # Add conversation handler with the states CHOOSING, TYPING_CHOICE and TYPING_REPLY conv_handler = ConversationHandler( entry_points=[CommandHandler('start', start)], states={ CHOOSING: [ MessageHandler(Filters.regex('^Want an Explaination$'), exp), MessageHandler(Filters.regex('^Go to the Project$'), project), MessageHandler(Filters.regex('^I would Submit a Translation$'), submit) ], TYPING_CHOICE: [ MessageHandler( Filters.text & ~(Filters.command | Filters.regex('^Done$')), start) ], TYPING_REPLY: [ MessageHandler(Filters.text & ~(Filters.command | Filters.regex('^Done$')), received_information) ] }, fallbacks=[MessageHandler(Filters.regex('^Done$'), done)], name="my_conversation", persistent=True, ) dp.add_handler(conv_handler) show_data_handler = CommandHandler('show_data', show_data) dp.add_handler(show_data_handler) # Start the Bot updater.start_polling() # Run the bot until you press Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle() if __name__ == '__main__': main()
2020-11-16 16:35:55,653 - apscheduler.scheduler - INFO - Scheduler started