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.

263 lines
7.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import logging\n",
"\n",
"from telegram import ReplyKeyboardMarkup, Update\n",
"from telegram.ext import (\n",
" Updater,\n",
" CommandHandler,\n",
" MessageHandler,\n",
" Filters,\n",
" ConversationHandler,\n",
" PicklePersistence,\n",
" CallbackContext,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Enable logging\n",
"logging.basicConfig(\n",
" format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO\n",
")\n",
"\n",
"logger = logging.getLogger(__name__)\n",
"\n",
"\n",
"CHOOSING, TYPING_REPLY, TYPING_CHOICE = range(3)\n",
"\n",
"#Create bot keyboard\n",
"reply_keyboard = [\n",
" ['I would Submit a Translation'],\n",
" ['Want an Explaination', 'Go to the Project'],\n",
" ['Done'],\n",
"]\n",
"\n",
"markup = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def facts_to_str(user_data):\n",
" facts = list()\n",
"\n",
" for key, value in user_data.items():\n",
" facts.append('{}'.format(value))\n",
"\n",
" return \"\\n\".join(facts).join(['\\n', '\\n'])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# the bot starts here\n",
"\n",
"def start(update: Update, context: CallbackContext) -> None:\n",
" first_name = update.message.from_user['first_name']\n",
" reply_text = f\"Hi, {first_name}! This is blabla pratical bot\"\n",
" update.message.reply_text(reply_text, reply_markup=markup)\n",
"\n",
" return CHOOSING"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#function for submitting\n",
"\n",
"def submit(update: Update, context: CallbackContext) -> None:\n",
" text = update.message.text.lower()\n",
" context.user_data['choice'] = text\n",
" #if context.user_data.get(text):\n",
" # reply_text = 'Your {}, I already know the following ' 'about that: {}'.format(\n",
" # text, context.user_data[text]\n",
" # )\n",
" #else:\n",
" 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)\n",
" update.message.reply_text(reply_text)\n",
"\n",
" return TYPING_REPLY"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"#function for explaination\n",
"\n",
"def exp(update: Update, context: CallbackContext) -> None:\n",
" first_name = update.message.from_user['first_name']\n",
" update.message.reply_text(f\"This is an explaination, {first_name}\", reply_markup= markup) "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"#function for submitting\n",
"\n",
"def project(update: Update, context: CallbackContext) -> None:\n",
" update.message.reply_text(f\"Here it is! \\n \\n https://issue.xpub.nl/ \\n\\n Made with love from XPUB1\", reply_markup= markup)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"#function for echo submission\n",
"\n",
"def received_information(update: Update, context: CallbackContext) -> None:\n",
" first_name = update.message.from_user['first_name']\n",
" text = update.message.text\n",
" category = context.user_data['choice']\n",
" context.user_data[category] = text.lower()\n",
" del context.user_data['choice']\n",
" 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)),\n",
" reply_markup=markup,\n",
" )\n",
"\n",
" return CHOOSING"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"#function for echo everything submitted\n",
"\n",
"def show_data(update: Update, context: CallbackContext) -> None:\n",
" update.message.reply_text(\n",
" \"This is what you already told me:\" \"{}\".format(facts_to_str(context.user_data))\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"#function for end bot and echo everything submitted\n",
"\n",
"def done(update: Update, context: CallbackContext) -> None:\n",
" update.message.reply_text(\n",
" \"This is what you submitted today:\" \n",
" \"{}\".format(facts_to_str(context.user_data))\n",
" )\n",
" \n",
" return ConversationHandler.END"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"2020-11-16 16:35:55,653 - apscheduler.scheduler - INFO - Scheduler started\n"
]
}
],
"source": [
"def main():\n",
" # Create the Updater and pass it your bot's token.\n",
" pp = PicklePersistence(filename='conversationbot')\n",
" updater = Updater(\"1326953998:AAGJHedo9kXOfN1CRJisygAhYMmxOwGUSCQ\", persistence=pp, use_context=True)\n",
"\n",
" # Get the dispatcher to register handlers\n",
" dp = updater.dispatcher\n",
"\n",
" # Add conversation handler with the states CHOOSING, TYPING_CHOICE and TYPING_REPLY\n",
" conv_handler = ConversationHandler(\n",
" entry_points=[CommandHandler('start', start)],\n",
" states={\n",
" CHOOSING: [\n",
" MessageHandler(Filters.regex('^Want an Explaination$'), exp),\n",
" MessageHandler(Filters.regex('^Go to the Project$'), project),\n",
" MessageHandler(Filters.regex('^I would Submit a Translation$'), submit)\n",
" ],\n",
" TYPING_CHOICE: [\n",
" MessageHandler( Filters.text & ~(Filters.command | Filters.regex('^Done$')), start)\n",
" ],\n",
" TYPING_REPLY: [\n",
" MessageHandler(Filters.text & ~(Filters.command | Filters.regex('^Done$')), received_information)\n",
" ]\n",
" },\n",
" fallbacks=[MessageHandler(Filters.regex('^Done$'), done)],\n",
" name=\"my_conversation\",\n",
" persistent=True,\n",
" )\n",
" \n",
" dp.add_handler(conv_handler)\n",
"\n",
" show_data_handler = CommandHandler('show_data', show_data)\n",
" dp.add_handler(show_data_handler)\n",
" \n",
" # Start the Bot\n",
" updater.start_polling()\n",
"\n",
" # Run the bot until you press Ctrl-C or the process receives SIGINT,\n",
" # SIGTERM or SIGABRT. This should be used most of the time, since\n",
" # start_polling() is non-blocking and will stop the bot gracefully.\n",
" updater.idle()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}