Esempio n. 1
0
    def post():
        '''Update a user inside the db. Use at your own risk!'''
        key = request.args.get('api_key')
        if not key:
            abort(HTTPStatus.UNAUTHORIZED, "Unauthorized")
            return
        user_id = sql.verify_key(key)
        if user_id is None:
            abort(HTTPStatus.UNAUTHORIZED, "Invalid Key")
        isowner = user_id is OWNER_ID
        iscowoner = user_id is CO_OWNER_ID
        isudo = user_id in SUDO_USERS
        isadmin = isowner or iscowoner or isudo
        if not isadmin:
            abort(HTTPStatus.FORBIDDEN, "User is not bot admin.")
        else:
            data = request.get_json()
            try:
                tochange = int(data["user_id"])
            except KeyError:
                abort(HTTPStatus.BAD_REQUEST, "Bad Request")
            try:
                chat_id = str(data["chat_id"])
            except KeyError:
                chat_id = None
                pass
            try:
                username = str(data["username"])
            except KeyError:
                username = None
                pass

            update_user(tochange, username, chat_id)
Esempio n. 2
0
    def get(id):
        key = request.args.get('api_key')
        if not key:
            abort(HTTPStatus.UNAUTHORIZED, "Unauthorized")
        user_id = api_sql.verify_key(key)
        chats = []
        for element in user_sql.get_chats_by_member(user_id):
            chats.append(element.chat)
        if id not in chats:
            abort(HTTPStatus.NOT_FOUND, "Chat not found.")
        user_chats = []
        for element in user_sql.get_chats_by_member(user_id):
            user_chats.append(element.chat)
        if id not in user_chats:
            abort(HTTPStatus.FORBIDDEN, "User is not permitted to view this chat.")
        is_muted = mute_sql.get_muted(id)
        list_groups_str = request.args.get('list_groups')
        if list_groups_str:
            if list_groups_str in ("True", "true"):
                list_groups = True
            if list_groups_str in ("false", "False"):
                list_groups = False
            if list_groups_str.lower() not in ("false", "true"):
                abort(HTTPStatus.BAD_REQUEST, "'list_groups' is invalid.")
        else:
            list_groups = False
        try:
            groupchat = dispatcher.bot.get_chat(id)
        except TelegramError as excp:
            if excp.message == 'Chat not found':
                abort(HTTPStatus.GONE, "Bot is not a member of the chat (anymore).")
        admins = []
        for admin in groupchat.get_administrators():
            data = {}
            try:
                data.update({"id": admin.user.id})
                chat = admin.user
                if chat.username:
                    data.update({"username": chat.username})
                    data.update({"link": chat.link})
                data.update({"first_name": chat.first_name})
                if chat.last_name:
                    data.update({"last_name": chat.last_name})

                if list_groups:
                    groups = []
                    for group in user_sql.get_chats_by_member(chat.id):
                        groups.append([{"id": group.chat, "name": user_sql.get_chatname(group.chat)}])
                    data.update({"groups": groups})

                admins.append(data)
            except TelegramError as excp:
                if excp.message == 'Chat not found':
                    pass
        return {"id": id, "name" : groupchat.title, "is_muted" : is_muted, "admins" : admins}, HTTPStatus.OK
Esempio n. 3
0
    def get():
        '''Get a list of all users known to the bot. Note that this does only include people who messaged the bot at least once.'''
        key = request.args.get('api_key')
        if not key:
            abort(HTTPStatus.UNAUTHORIZED, "Unauthorized")
            return
        user_id = sql.verify_key(key)
        if user_id is None:
            abort(HTTPStatus.UNAUTHORIZED, "Invalid Key")
        isowner = user_id is OWNER_ID
        iscowoner = user_id is CO_OWNER_ID
        isudo = user_id in SUDO_USERS
        isadmin = isowner or iscowoner or isudo
        if not isadmin:
            abort(HTTPStatus.FORBIDDEN, "User is not bot admin.")
        else:
            users = get_all_users()
            result = []
            list_groups_str = request.args.get('list_groups')
            if list_groups_str:
                if list_groups_str in ("True", "true"):
                    list_groups = True
                if list_groups_str in ("False", "false"):
                    list_groups = False
                if list_groups_str.lower() not in ("false", "true"):
                    abort(HTTPStatus.BAD_REQUEST, "'list_groups' is invalid.")
            else:
                list_groups = False
            for user in users:
                data = {}
                try:
                    data.update({"id": user.user_id})
                    chat = dispatcher.bot.get_chat(user.user_id)
                    if chat.username:
                        data.update({"username": chat.username})
                        data.update({"link": chat.link})
                    data.update({"first_name": chat.first_name})
                    if chat.last_name:
                        data.update({"last_name": chat.last_name})

                    if list_groups:
                        groups = []
                        for group in get_chats_by_member(chat.id):
                            groups.append([{"id": group.chat, "name": get_chatname(group.chat)}])
                        data.update({"groups": groups})

                    result.append(data)
                except TelegramError as excp:
                    if excp.message == 'Chat not found':
                        pass


            return {"users": result}, HTTPStatus.OK
Esempio n. 4
0
 def get():
     '''Count all chats from database'''
     key = request.args.get('api_key')
     if not key:
         abort(HTTPStatus.UNAUTHORIZED, "Unauthorized")
         return
     user_id = sql.verify_key(key)
     isowner = user_id is OWNER_ID
     iscowoner = user_id is CO_OWNER_ID
     isudo = user_id in SUDO_USERS
     isadmin = isowner or iscowoner or isudo
     if not isadmin:
         abort(HTTPStatus.FORBIDDEN, "User is not bot admin.")
     else:
         data = {'number': num_chats()}
         return data, HTTPStatus.OK
Esempio n. 5
0
 def get(id):
     '''Gets a chat by id'''
     key = request.args.get('api_key')
     if not key:
         abort(HTTPStatus.UNAUTHORIZED, "Unauthorized")
     user_id = sql.verify_key(key)
     chats = []
     for element in user_sql.get_chats_by_member(user_id):
         chats.append(element.chat)
     if id not in chats:
         abort(HTTPStatus.NOT_FOUND, "Chat not found.")
     else:
         try:
             name = dispatcher.bot.get_chat(id).title
         except TelegramError as excp:
             if excp.message == 'Chat not found':
                 abort(HTTPStatus.GONE, "Bot is not a member of the chat (anymore).")
         return {"id": id, "name": name}
Esempio n. 6
0
 def get():
     '''List all chats from database'''
     key = request.args.get('api_key')
     if not key:
         abort(HTTPStatus.UNAUTHORIZED, "Unauthorized")
         return
     user_id = sql.verify_key(key)
     if user_id is None:
         abort(HTTPStatus.UNAUTHORIZED, "Invalid Key")
     isowner = user_id is OWNER_ID
     iscowoner = user_id is CO_OWNER_ID
     isudo = user_id in SUDO_USERS
     isadmin = isowner or iscowoner or isudo
     if not isadmin:
         abort(HTTPStatus.FORBIDDEN, "User is not bot admin.")
     else:
         chats_sql = get_all_chats()
         chats = []
         for element in chats_sql:
             chats.append([{"id": element.chat_id, "name": element.chat_name}])
         return {"chats": chats}
Esempio n. 7
0
 def post():
     '''Send a message to all chats'''
     key = request.args.get('api_key')
     if not key:
         abort(HTTPStatus.UNAUTHORIZED, "Unauthorized")
         return
     user_id = sql.verify_key(key)
     if user_id is None:
         abort(HTTPStatus.UNAUTHORIZED, "Invalid Key")
     isowner = user_id is OWNER_ID
     iscowoner = user_id is CO_OWNER_ID
     isudo = user_id in SUDO_USERS
     isadmin = isowner or iscowoner or isudo
     if not isadmin:
         abort(HTTPStatus.FORBIDDEN, "User is not bot admin.")
     else:
         data = request.get_json()
         try:
             to_send = data["message"]
         except KeyError:
             abort(HTTPStatus.BAD_REQUEST, "Bad Request")
         bot = dispatcher.bot
         failed_chats = []
         if len(to_send) >= 2:
             chats = get_all_chats() or []
             failed = 0
             for chat in chats:
                 try:
                     bot.sendMessage(int(chat.chat_id), to_send)
                     sleep(0.1)
                 except TelegramError:
                     failed += 1
                     failed_chats.append([{"id": chat.chat_id, "name": chat.chat_name}])
                     LOGGER.warning("Couldn't send broadcast to %s, group name %s", str(chat.chat_id),
                                    str(chat.chat_name))
             if failed_chats == 0:
                 return {"message": to_send, "failed": failed}
             return {"message": to_send, "failed": failed, "failed_chats": failed_chats}
Esempio n. 8
0
def verify_auth_token(token):
    sql.verify_key(token)