def deal_with_request(request_json):
    # retrieve the message in JSON and then transform it to Telegram object
    update_obj = telegram.Update.de_json(request_json, BOT)
    message_obj = update_obj.message    
    user_obj = message_obj.from_user
    chat_id = user_obj.id    
    username = user_obj.username
    last_name = user_obj.last_name if user_obj.last_name else ''
    name = (user_obj.first_name + ' ' + last_name).strip()

    user = bot_ndb_user.get_person_by_id_and_application(user_obj.id, 'telegram')

    if user == None:
        user = bot_ndb_user.add_person(chat_id, name, last_name, username, 'telegram')
        report_master('New user: {}'.format(user.get_first_last_username()))
    else:
        _, was_disabled = user.update_info(name, last_name, username)
        if was_disabled:
            msg = "Bot riattivato!"
            send_message(user, msg)
    
    if message_obj.text:
        if deal_with_commands(user, message_obj.text):
            return

    repeat_state(user, message_obj=message_obj)
def repeat_state(user, message_obj=None):
    state = user.state
    if state is None:
        restart(user)
        return
    method = possibles.get(state)
    if not method:
        msg = "⚠️ User {} sent to unknown method state: {}".format(user.chat_id, state)
        report_master(msg)
        restart(user)
    else:
        method(user, message_obj)
 def dec(*args, **kwargs):
     try:
         func(*args, **kwargs)
     except Exception:
         report_string = '❗️ Exception {}'.format(traceback.format_exc()) #.splitlines()
         logging.error(report_string)          
         try:  
             report_master(report_string)
             send_message(user, report_string)
         except Exception:
             report_string = '❗️ Exception {}'.format(traceback.format_exc())
             logging.error(report_string)
             send_message(user, report_string)
Ejemplo n.º 4
0
def add_user():
    from bot_firestore_user import User
    from bot_telegram import report_master
    user_id = request.form.get('id')
    user_name = request.form.get('name')
    logging.debug('ENDOPOINT: add_user id={} name={}'.format(
        user_id, user_name))
    if user_id and user_name:
        if user_id.startswith('web_') and len(user_id) > 4:
            application, serial_id = user_id.split('_')
            if User.get_user(application, serial_id):
                return jsonify({'success': False, 'error': 'User exists'}), 400
            u = User.create_user(application, serial_id, user_name)
            u.set_state('state_INITIAL')
            report_master('New user: {}'.format(user_id))
            return jsonify({'success': True, 'error': None}), 200
        else:
            error_msg = 'id must conform to string pattern <web_serial>'
            return jsonify({'success': False, 'error': error_msg}), 400
    error_msg = 'Both id and name params need to be specified'
    return jsonify({'success': False, 'error': error_msg}), 400