Ejemplo n.º 1
0
def send_to_user_or_group(user_id, message):
    try:
        bot.send_message(user_id, message)
    except TelegramError as e:
        try:
            print(e)
            formatted_msg = utils.get_username(user_id, bot) + ": " + message
            bot.send_message(storage.get_target_chat(),
                             formatted_msg,
                             parse_mode=ParseMode.HTML)
        except TelegramError as e:
            print("Well i tried hard enough " + e.message)
Ejemplo n.º 2
0
def ban_user(update, ctx, args, query):
    user_id = int(args[1])
    msg_id = int(args[2])
    bot.delete_message(storage.get_target_chat(), msg_id)
    if storage.is_banned(user_id):
        query.edit_message_text(text=storage.get_string("USER_ALREADY_BANNED"))
    elif not storage.is_admin(user_id):
        storage.ban_user(user_id)
        logging.info("Banned user %s" % user_id)
        query.edit_message_text(text=storage.get_string("USER_BANNED"))
    else:
        query.edit_message_text(text=storage.get_string("USER_IS_ADMIN"))
Ejemplo n.º 3
0
def get_username(user_id, bot):
    try:
        chat = bot.get_chat_member(storage.get_target_chat(), user_id)
        user = chat.user
        if user.username != None:
            return "@" + user.username
        else:
            user_name = user.first_name + \
                (" " + user.last_name if user.last_name else "")
            telegram_html = f"<a href='tg://user?id={user_id}'>{user_name}</a>"
            return telegram_html
    except TelegramError as e:
        logging.error("Error trying to get the chat member with id " +
                      user_id + ": " + e.message)
    return "UsernameNotFound"
Ejemplo n.º 4
0
def send_message(update, ctx):
    message_text = utils.format_message(update.message)
    if len(message_text) == 0:
        update.message.reply_text(storage.get_string("EMPTY_MSG"))
    else:
        try:
            bot.send_message(storage.get_target_chat(),
                             message_text,
                             reply_markup=utils.make_report_keyboard(
                                 update.message.from_user.id),
                             parse_mode=ParseMode.HTML)
            update.message.reply_text(storage.get_string("MSG_SENT"))
        except Exception as e:
            template = Template(storage.get_string("CANT_SEND"))
            update.message.reply_text(
                template.safe_substitute(message=e.message))
Ejemplo n.º 5
0
def warn_user(update, ctx, args, query):
    user_id = int(args[1])
    msg_id = int(args[2])
    should_hide_msg = bool(int(args[3]))
    warns = storage.get_warns_for_user(user_id)
    if user_id in storage.get_admin_set():
        query.edit_message_text(text=storage.get_string("USER_IS_ADMIN"))
    else:
        if msg_id in warns:
            query.edit_message_text(
                text=storage.get_string("WARN_WARNED_ALREADY_2"))
        else:
            query.edit_message_text(
                text=storage.get_string("WARN_WRITE_REASON"))
            warn_initiator = query.from_user.id
            awaiting_warn_reason[warn_initiator] = (user_id, msg_id)
        if should_hide_msg:
            bot.delete_message(storage.get_target_chat(), msg_id)
Ejemplo n.º 6
0
def user_is_in_group(user_id, bot):
    try:
        chat = bot.get_chat_member(storage.get_target_chat(), user_id)
        return chat.status != ChatMember.LEFT and chat.status != ChatMember.KICKED and chat.status != ChatMember.RESTRICTED
    except TelegramError as e:
        return False