Пример #1
0
def request_access(request_data):
    conn,cursor = connect()
    chatId = str(request_data['message']['chat']['id'])
    firstName = ""
    if ('first_name' in request_data['message']['chat']):
        firstName = request_data['message']['chat']['first_name']
    lastName = ""
    if ('last_name' in request_data['message']['chat']):
        lastName = request_data['message']['chat']['last_name']
    tusername = request_data['message']['from']['username']
    message = ""
    try:
        query = "select * from pending_requests where chatId = {0}".format(chatId)
        result = cursor.execute(query)
        if (result > 0):
            message = "You have already requested for access. Please wait patiently the admin will soon accept your request\n"
        else:
            query = "select * from accepted_users where chatId = {0}".format(chatId)
            result = cursor.execute(query)
            if (result > 0):
                message = "You already have access\n"
            else:
                query = "insert into pending_requests (chatId,firstName,lastName,tusername) values ('{0}','{1}','{2}','{3}');".format(chatId,firstName,lastName,tusername)
                cursor.execute(query)
                conn.commit()
                send_admin_message("You have a pending request from " + tusername)
                message = "The admin will be notified about your request to use this bot. Once he accepts it you will be able to get emails. You can keep modifiying your preferences till then"
        send_message(chatId,message)
        conn.close()
    except (MySQLdb.Error, MySQLdb.Warning) as e:
        app.logger.info(e)
        send_error_message(chatId)
        conn.close()
Пример #2
0
def webhook():
    request_data = request.get_json()
    chatId = ""
    if ('message' in request_data):
        try:
            chatId = str(request_data['message']['chat']['id'])
        except KeyError:
            try:
                chatId = str(request_data['message']['from']['id'])
            except KeyError:
                send_admin_message(str(request_data))
                return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
    else:
        return json.dumps({'success':True}), 200, {'ContentType':'application/json'}    #As of now edited messages are not supported
    if (not exists_in_db(chatId)):
        add_entry_to_user_in_db(request_data)
    handle_request(request_data)
    return json.dumps({'success':True}), 200, {'ContentType':'application/json'}