Esempio n. 1
0
def enable_welcome(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):

        group = update_group(update.message.chat)
        group.welcome_enabled = True
        Session.add(group)
        Session.commit()
        send_async(bot, chat_id=update.message.chat.id, text='Welcome enabled')
Esempio n. 2
0
def list_triggers(bot, update):
    triggers = Session.query(Trigger).all()
    local_triggers = Session.query(LocalTrigger).filter_by(
        chat_id=update.message.chat.id).all()
    msg = 'List of current triggers: \n' + \
          '<b>Global:</b>\n' + ('\n'.join([trigger.trigger for trigger in triggers]) or '[Empty]\n') + \
          '\n<b>Local:</b>\n' + ('\n'.join([trigger.trigger for trigger in local_triggers]) or '[Empty]\n')
    send_async(bot,
               chat_id=update.message.chat.id,
               text=msg,
               parse_mode=ParseMode.HTML)
Esempio n. 3
0
def send_async(bot: Bot, *args, **kwargs):
    try:
        return bot.sendMessage(*args, **kwargs)

    except TelegramError as err:
        bot.logger.error(err.message)

        group = Session.query(Group).filter_by(id=kwargs['chat_id']).first()
        if group is not None:
            group.bot_in_group = False
            Session.add(group)
            Session.commit()
        return None
Esempio n. 4
0
def show_welcome(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):

        group = update_group(update.message.chat)
        welcome_msg = Session.query(WelcomeMsg).filter_by(
            chat_id=group.id).first()
        if welcome_msg is None:
            welcome_msg = WelcomeMsg(chat_id=group.id,
                                     message='Hi, %username%!')
            Session.add(welcome_msg)
            Session.commit()

        send_async(bot, chat_id=group.id, text=welcome_msg.message)
Esempio n. 5
0
def list_admins(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        admins = Session.query(Admin).filter(
            Admin.admin_group == update.message.chat.id).all()
        users = []
        for admin_user in admins:
            users.append(
                Session.query(User).filter_by(id=admin_user.user_id).first())
        msg = 'Administrators list:\n'
        for user in users:
            msg += '{} @{} {} {}\n'.format(user.id, user.username,
                                           user.first_name, user.last_name)

        send_async(bot, chat_id=update.message.chat.id, text=msg)
Esempio n. 6
0
def del_trigger(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        msg = update.message.text.split(' ', 1)[1]
        trigger = Session.query(LocalTrigger).filter_by(trigger=msg).first()
        if trigger is not None:
            Session.delete(trigger)
            Session.commit()
            send_async(
                bot,
                chat_id=update.message.chat.id,
                text='The trigger for "{}" has been deleted.'.format(msg))
        else:
            send_async(bot,
                       chat_id=update.message.chat.id,
                       text='Where did you see such a trigger? 0_o')
Esempio n. 7
0
def add_trigger(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        msg = update.message.text.split(' ', 1)
        if len(msg) == 2 and len(
                msg[1]) > 0 or update.message.reply_to_message:
            trigger_text = msg[1].strip()
            trigger = Session.query(Trigger).filter_by(
                trigger=trigger_text).first()
            if trigger is None:
                data = update.message.reply_to_message
                add_trigger_db(data, trigger_text)
                send_async(
                    bot,
                    chat_id=update.message.chat.id,
                    text='The trigger for the phrase "{}" is set.'.format(
                        trigger_text))
            else:
                send_async(
                    bot,
                    chat_id=update.message.chat.id,
                    text='Trigger "{}" already exists, select another one.'.
                    format(trigger_text))
        else:
            send_async(bot,
                       chat_id=update.message.chat.id,
                       text='Your thoughts are not clear, try one more time')
Esempio n. 8
0
def del_adm(bot, chat_id, user):
    adm = Session.query(Admin).filter_by(user_id=user.id,
                                         admin_group=chat_id).first()

    if adm is None:
        send_async(bot,
                   chat_id=chat_id,
                   text='@{} never had any power here!'.format(user.username))

    else:
        Session.delete(adm)
        Session.commit()
        send_async(bot,
                   chat_id=chat_id,
                   text='@{}, now you have no power here!'.format(
                       user.username))
Esempio n. 9
0
def set_welcome(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):

        group = update_group(update.message.chat)
        welcome_msg = Session.query(WelcomeMsg).filter_by(
            chat_id=group.id).first()
        if welcome_msg is None:
            welcome_msg = WelcomeMsg(chat_id=group.id,
                                     message=update.message.text.split(' ',
                                                                       1)[1])
        else:
            welcome_msg.message = update.message.text.split(' ', 1)[1]
        Session.add(welcome_msg)
        Session.commit()
        send_async(bot,
                   chat_id=update.message.chat.id,
                   text='The welcome text is set.')
Esempio n. 10
0
def trigger_show(bot: Bot, update: Update):
    trigger = Session.query(LocalTrigger).filter_by(
        chat_id=update.message.chat.id, trigger=update.message.text).first()
    if trigger is None:
        trigger = Session.query(Trigger).filter_by(
            trigger=update.message.text).first()
    if trigger is not None:
        if trigger.message_type == MessageType.AUDIO.value:
            bot.send_audio(update.message.chat.id, trigger.message)
        elif trigger.message_type == MessageType.DOCUMENT.value:
            bot.send_document(update.message.chat.id, trigger.message)
        elif trigger.message_type == MessageType.VOICE.value:
            bot.send_voice(update.message.chat.id, trigger.message)
        elif trigger.message_type == MessageType.STICKER.value:
            bot.send_sticker(update.message.chat.id, trigger.message)
        elif trigger.message_type == MessageType.CONTACT.value:
            msg = trigger.message.replace('\'', '"')
            contact = loads(msg)
            if 'phone_number' not in contact.keys():
                contact['phone_number'] = None
            if 'first_name' not in contact.keys():
                contact['first_name'] = None
            if 'last_name' not in contact.keys():
                contact['last_name'] = None
            bot.send_contact(update.message.chat.id, contact['phone_number'],
                             contact['first_name'], contact['last_name'])
        elif trigger.message_type == MessageType.VIDEO.value:
            bot.send_video(update.message.chat.id, trigger.message)
        elif trigger.message_type == MessageType.VIDEO_NOTE.value:
            bot.send_video_note(update.message.chat.id, trigger.message)
        elif trigger.message_type == MessageType.LOCATION.value:
            msg = trigger.message.replace('\'', '"')
            location = loads(msg)
            bot.send_location(update.message.chat.id, location['latitude'],
                              location['longitude'])
        elif trigger.message_type == MessageType.PHOTO.value:
            bot.send_photo(update.message.chat.id, trigger.message)
        else:
            send_async(bot,
                       chat_id=update.message.chat.id,
                       text=trigger.message,
                       disable_web_page_preview=True)
Esempio n. 11
0
def add_trigger_db(message: Message, trigger_text: str):

    trigger = Session.query(Trigger).filter_by(trigger=trigger_text).first()
    if trigger is None:
        trigger = Trigger()
        trigger.trigger = trigger_text

    if message.audio:
        trigger.message = msg.audio.file_id
        trigger.message_type = MessageType.AUDIO.value
    elif msg.document:
        trigger.message = msg.document.file_id
        trigger.message_type = MessageType.DOCUMENT.value
    elif msg.voice:
        trigger.message = msg.voice.file_id
        trigger.message_type = MessageType.VOICE.value
    elif msg.sticker:
        trigger.message = msg.sticker.file_id
        trigger.message_type = MessageType.STICKER.value
    elif msg.contact:
        trigger.message = str(msg.contact)
        trigger.message_type = MessageType.CONTACT.value
    elif msg.video:
        trigger.message = msg.video.file_id
        trigger.message_type = MessageType.VIDEO.value
    elif msg.video_note:
        trigger.message = msg.video_note.file_id
        trigger.message_type = MessageType.VIDEO_NOTE.value
    elif msg.location:
        trigger.message = str(msg.location)
        trigger.message_type = MessageType.LOCATION.value
    elif msg.photo:
        trigger.message = msg.photo[-1].file_id
        trigger.message_type = MessageType.PHOTO.value
    else:
        trigger.message = msg.text
        trigger.message_type = MessageType.TEXT.value

    Session.add(trigger)
    Session.commit()
Esempio n. 12
0
def set_admin(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        msg = update.message.text.split(' ', 1)[1]
        msg = msg.replace('@', '')
        if msg != '':
            # bot.getChatMember(update.message.chat_id, str(update.message.user.username))
            user = Session.query(User).filter_by(username=msg).first()

            # if user is None:
            # send_async(bot,
            #     chat_id=update.message.chat.id,
            #   text='No such user')

            #  else:
            adm = Session.query(Admin).filter_by(
                user_id=user.id, admin_group=update.message.chat.id).first()

            if adm is None:
                new_group_admin = Admin(user_id=user.id,
                                        admin_type=AdminType.GROUP.value,
                                        admin_group=update.message.chat.id)

                Session.add(new_group_admin)
                Session.commit()
                send_async(bot,
                           chat_id=update.message.chat.id,
                           text="""Welcome our new administrator: @{}!
    Check the commands list with /help command""".format(user.username))

            else:
                send_async(bot,
                           chat_id=update.message.chat.id,
                           text='@{} already has administrator rights'.format(
                               user.username))
Esempio n. 13
0
def del_admin(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        msg = update.message.text.split(' ', 1)[1]
        if msg.find('@') != -1:
            msg = msg.replace('@', '')
            if msg != '':
                user = Session.query(User).filter_by(username=msg).first()
                if user is None:
                    send_async(bot,
                               chat_id=update.message.chat.id,
                               text='No such user')

                else:
                    del_adm(bot, update.message.chat.id, user, session)
        else:
            user = Session.query(User).filter_by(id=msg).first()
            if user is None:
                send_async(bot,
                           chat_id=update.message.chat.id,
                           text='No such user')

            else:
                del_adm(bot, update.message.chat.id, user)
Esempio n. 14
0
def update_group(grp):
    if grp.type in ['group']:
        group = Session.query(Group).filter_by(id=grp.id).first()
        if group is None:
            group = Group(id=grp.id, title=grp.title, username=grp.username)
            Session.add(group)

        else:
            updated = False
            if group.username != grp.username:
                group.username = grp.username
                updated = True
            if group.title != grp.title:
                group.title = grp.title
                updated = True
            if not group.bot_in_group:
                group.bot_in_group = True
                updated = True
            if updated:
                Session.add(group)

        Session.commit()
        return group
    return None
Esempio n. 15
0
def unban(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        username = update.message.text.split(' ', 1)[1]
        username = username.replace('@', '')
        user = Session.query(User).filter_by(username=username).first()
        if user:
            banned = Session.query(Ban).filter_by(user_id=user.id).first()
            if banned:
                Session.delete(banned)
                Session.commit()
                send_async(bot, chat_id=user.id, text='We can talk again 🌚')
                send_async(
                    bot,
                    chat_id=update.message.chat.id,
                    text='{} is no longer banned.'.format('@' + user.username))
            else:
                send_async(bot,
                           chat_id=update.message.chat.id,
                           text='This soldier is not banned')
        else:
            send_async(bot,
                       chat_id=update.message.chat.id,
                       text='No such user')
Esempio n. 16
0
from telegram import Bot
from telegram.error import TelegramError
from telegram.ext.dispatcher import run_async
from Dbtables import Session, Group

Session()


@run_async
def send_async(bot: Bot, *args, **kwargs):
    try:
        return bot.sendMessage(*args, **kwargs)

    except TelegramError as err:
        bot.logger.error(err.message)

        group = Session.query(Group).filter_by(id=kwargs['chat_id']).first()
        if group is not None:
            group.bot_in_group = False
            Session.add(group)
            Session.commit()
        return None


def update_group(grp):
    if grp.type in ['group']:
        group = Session.query(Group).filter_by(id=grp.id).first()
        if group is None:
            group = Group(id=grp.id, title=grp.title, username=grp.username)
            Session.add(group)
Esempio n. 17
0
def ban(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        username, reason = update.message.text.split(' ', 2)[1:]
        username = username.replace('@', '')
        user = Session.query(User).filter_by(username=username).first()
        if user:
            banned = Session.query(Ban).filter_by(user_id=user.id).first()
            if banned:
                send_async(
                    bot,
                    chat_id=update.message.chat.id,
                    text='This user is already banned. The reason is: .'.
                    format(banned.to_date, banned.reason))
            else:
                banned = Ban()
                banned.user_id = user.id
                banned.from_date = datetime.now()
                banned.to_date = datetime.max
                banned.reason = reason or 'Reason not specified'
                member = Session.query().filter_by(user_id=user.id).first()
                if member:
                    Session.delete(member)
                admins = Session.query(Admin).filter_by(user_id=user.id).all()
                for admin in admins:
                    Session.delete(admin)
                Session.add(banned)
                Session.commit()
                send_async(bot,
                           chat_id=user.id,
                           text='You were banned because: {}'.format(
                               banned.reason))
                send_async(bot,
                           chat_id=update.message.chat.id,
                           text='Soldier successfully banned')
        else:
            send_async(bot,
                       chat_id=update.message.chat.id,
                       text='No such user')