{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import telegram\n", "from telegram.ext import (\n", " Updater,\n", " CommandHandler,\n", " MessageHandler,\n", " Filters,\n", " ConversationHandler,\n", " PicklePersistence,\n", " CallbackContext,\n", ")\n", "\n", "import re\n", "import logging\n", "import json\n", "\n", "logging.basicConfig(level=logging.INFO,\n", " format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n", "\n", "# The API Key we received for our bot\n", "API_KEY = \"1452534708:AAERTrbN_LUDSLkf0J8sJjvMEEuwfRNka50\"\n", "# Create an updater object with our API Key\n", "updater = telegram.ext.Updater(API_KEY)\n", "# Retrieve the dispatcher, which will be used to add handlers\n", "dispatcher = updater.dispatcher\n", "\n", "\n", "# Our states, as integers\n", "START = 0\n", "WELCOME = 1\n", "QUESTION = 2\n", "CANCEL = 3\n", "INPUT = 4\n", "EXPLAIN = 5\n", "\n", "\n", "# The entry function\n", "def start(update_obj, context):\n", " first_name = update_obj.message.from_user['first_name']\n", " # send the question, and show the keyboard markup (suggested answers)\n", " update_obj.message.reply_text(f\"Hello {first_name}, what would you like to do? :)\",\n", " reply_markup=telegram.ReplyKeyboardMarkup([['explaination','submit', 'project','end']], one_time_keyboard=True)\n", " )\n", " # go to the WELCOME state\n", " return WELCOME\n", "\n", "#def randomize_numbers(user_data):\n", " #key = input()\n", " \n", " \n", " # facts = list()\n", "\n", " # for key, value in user_data.items():\n", " # facts.append('{} - {}'.format(key, value))\n", "\n", " # return \"\\n\".join(facts).join(['\\n', '\\n'])\n", " #update_obj.message.reply_text()\n", " \n", " \n", "\n", "\n", "# in the WELCOME state, check what the user wants to do\n", "def welcome(update_obj, context):\n", "\n", " if update_obj.message.text.lower() in ['explaination']:\n", " update_obj.message.reply_text(f\"This is an explaination\", reply_markup=telegram.ReplyKeyboardMarkup([['explaination','submit', 'project','end']], one_time_keyboard=True)) \n", " \n", " elif update_obj.message.text.lower() in ['submit']:\n", " #randomize_numbers(user_data)\n", " question(update_obj, context)\n", " \n", "\n", " \n", " elif update_obj.message.text.lower() in ['project']:\n", " update_obj.message.reply_text(f\"Here it is! \\n \\n https://issue.xpub.nl/\", reply_markup=telegram.ReplyKeyboardMarkup([['explaination','submit', 'project','end']], one_time_keyboard=True))\n", " else:\n", " update_obj.message.reply_text(f\"Okay, see you!\")\n", " return telegram.ext.ConversationHandler.END\n", " #telegram.ext.ConversationHandler.END\n", " \n", "\n", "# in the QUESTION state\n", "def question(update_obj, context):\n", " text = update_obj.message.text.lower()\n", " context.update_obj['choice'] = text\n", " if context.update_obj.get(text):\n", " reply_text = 'Thanks for the submission :)'\n", " return START\n", " \n", "\n", "def cancel(update_obj, context):\n", " # get the user's first name\n", " first_name = update_obj.message.from_user['first_name']\n", " update_obj.message.reply_text(\n", " f\"Okay, see you!\", reply_markup=telegram.ReplyKeyboardRemove())\n", " return telegram.ext.ConversationHandler.END" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# a regular expression that matches yes or no\n", "regex = re.compile(r'^(explaination|submit|project|end)$', re.IGNORECASE)\n", "# Create our ConversationHandler, with only one state\n", "handler = telegram.ext.ConversationHandler(\n", " entry_points=[telegram.ext.CommandHandler('start', start)],\n", " states={\n", " WELCOME: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(regex), welcome)],\n", " QUESTION: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(r'^\\d+$'), question)],\n", " \n", " CANCEL: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(regex), cancel)],\n", " #INPUT: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(regex), inputt)],\n", " START: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(regex), start)],\n", " # EXPLAIN: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(regex), explain)]\n", " },\n", " fallbacks=[telegram.ext.CommandHandler('cancel', cancel)],\n", " )\n", "# add the handler to the dispatcher\n", "dispatcher.add_handler(handler)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2020-11-16 13:16:12,619 - apscheduler.scheduler - INFO - Scheduler started\n", "2020-11-16 13:16:20,503 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.\n", "Traceback (most recent call last):\n", " File \"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telegram/ext/dispatcher.py\", line 425, in process_update\n", " handler.handle_update(update, self, check, context)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telegram/ext/conversationhandler.py\", line 487, in handle_update\n", " new_state = handler.handle_update(update, dispatcher, check_result, context)\n", " File \"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telegram/ext/handler.py\", line 145, in handle_update\n", " return self.callback(update, context)\n", " File \"\", line 69, in welcome\n", " question(update_obj, context)\n", " File \"\", line 84, in question\n", " context.update_obj['choice'] = text\n", "AttributeError: 'CallbackContext' object has no attribute 'update_obj'\n" ] } ], "source": [ "# start polling for updates from Telegram\n", "updater.start_polling()\n", "# block until a signal (like one sent by CTRL+C) is sent\n", "updater.idle()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(alora)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }