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.5 KiB

In [1]:
import telegram
from telegram.ext import (
    Updater,
    CommandHandler,
    MessageHandler,
    Filters,
    ConversationHandler,
    PicklePersistence,
    CallbackContext,
)

import re
import logging
import json

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# The API Key we received for our bot
API_KEY = "1452534708:AAERTrbN_LUDSLkf0J8sJjvMEEuwfRNka50"
# Create an updater object with our API Key
updater = telegram.ext.Updater(API_KEY)
# Retrieve the dispatcher, which will be used to add handlers
dispatcher = updater.dispatcher


# Our states, as integers
START = 0
WELCOME = 1
QUESTION = 2
CANCEL = 3
INPUT = 4
EXPLAIN = 5


# The entry function
def start(update_obj, context):
    first_name = update_obj.message.from_user['first_name']
    # send the question, and show the keyboard markup (suggested answers)
    update_obj.message.reply_text(f"Hello {first_name}, what would you like to do? :)",
        reply_markup=telegram.ReplyKeyboardMarkup([['explaination','submit', 'project','end']], one_time_keyboard=True)
    )
    # go to the WELCOME state
    return WELCOME

#def randomize_numbers(user_data):
    #key = input()
    
    
 #   facts = list()

  #  for key, value in user_data.items():
    #    facts.append('{} - {}'.format(key, value))

   # return "\n".join(facts).join(['\n', '\n'])
    #update_obj.message.reply_text()
    
    


# in the WELCOME state, check what the user wants to do
def welcome(update_obj, context):

    if update_obj.message.text.lower() in ['explaination']:
        update_obj.message.reply_text(f"This is an explaination", reply_markup=telegram.ReplyKeyboardMarkup([['explaination','submit', 'project','end']], one_time_keyboard=True)) 
    
    elif update_obj.message.text.lower() in ['submit']:
        #randomize_numbers(user_data)
        question(update_obj, context)
        

    
    elif update_obj.message.text.lower() in ['project']:
        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))
    else:
        update_obj.message.reply_text(f"Okay, see you!")
        return telegram.ext.ConversationHandler.END
    #telegram.ext.ConversationHandler.END
    

# in the QUESTION state
def question(update_obj, context):
    text = update_obj.message.text.lower()
    context.update_obj['choice'] = text
    if context.update_obj.get(text):
        reply_text = 'Thanks for the submission :)'
    return START
    

def cancel(update_obj, context):
    # get the user's first name
    first_name = update_obj.message.from_user['first_name']
    update_obj.message.reply_text(
        f"Okay, see you!", reply_markup=telegram.ReplyKeyboardRemove())
    return telegram.ext.ConversationHandler.END
In [2]:
# a regular expression that matches yes or no
regex = re.compile(r'^(explaination|submit|project|end)$', re.IGNORECASE)
# Create our ConversationHandler, with only one state
handler = telegram.ext.ConversationHandler(
      entry_points=[telegram.ext.CommandHandler('start', start)],
      states={
            WELCOME: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(regex), welcome)],
            QUESTION: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(r'^\d+$'), question)],
            
            CANCEL: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(regex), cancel)],
            #INPUT: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(regex), inputt)],
            START: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(regex), start)],
        #    EXPLAIN: [telegram.ext.MessageHandler(telegram.ext.Filters.regex(regex), explain)]
      },
      fallbacks=[telegram.ext.CommandHandler('cancel', cancel)],
      )
# add the handler to the dispatcher
dispatcher.add_handler(handler)
In [ ]:
# start polling for updates from Telegram
updater.start_polling()
# block until a signal (like one sent by CTRL+C) is sent
updater.idle()
2020-11-16 13:16:12,619 - apscheduler.scheduler - INFO - Scheduler started
2020-11-16 13:16:20,503 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telegram/ext/dispatcher.py", line 425, in process_update
    handler.handle_update(update, self, check, context)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telegram/ext/conversationhandler.py", line 487, in handle_update
    new_state = handler.handle_update(update, dispatcher, check_result, context)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telegram/ext/handler.py", line 145, in handle_update
    return self.callback(update, context)
  File "<ipython-input-1-ec625b5252c3>", line 69, in welcome
    question(update_obj, context)
  File "<ipython-input-1-ec625b5252c3>", line 84, in question
    context.update_obj['choice'] = text
AttributeError: 'CallbackContext' object has no attribute 'update_obj'
In [ ]:
 
In [ ]:
print(alora)
In [ ]: