Esempio n. 1
0
def check_subscription(bot: Bot, user_data: dict, channel_id: int):
    try:
        result = bot.get_chat_member(chat_id=channel_id,
                                     user_id=user_data["id"])
    except err.TelegramError as e:
        return False
    return result.status != result.LEFT and result.status != result.KICKED
Esempio n. 2
0
def try_add_channel(bot: Bot, candidate: str, channel_model_class):
    if candidate[0] != "@":
        candidate = "@" + candidate

    try:
        channel = bot.get_chat(chat_id=candidate)
    except err.TelegramError:
        return False

    if channel.type != "channel":
        return False

    chat_member = bot.get_chat_member(chat_id=candidate, user_id=bot.id)
    if chat_member.status not in (chat_member.ADMINISTRATOR,
                                  chat_member.CREATOR):
        return False

    session = db.Session()
    if session.query(channel_model_class).filter_by(id=channel.id).count() > 0:

        if channel_model_class is ChannelToSubscribe:
            session.query(channel_model_class).get(channel.id).enabled = True
            session.commit()
            session.close()
            return True

        # already exists
        session.close()
        return False

    new_channel = channel_model_class(id=channel.id)
    session.add(new_channel)
    session.commit()
    session.close()

    return True