コード例 #1
0
def photo_from_telegram(bot, update):
    if update.message:
        tg_group_id = update.message.chat_id  # telegram group id
        qq_group_id, _, forward_index = get_forward_index(tg_group_id=int(tg_group_id))

    file_id = update.message.photo[-1].file_id
    pic_url = tg_get_pic_url(file_id, 'jpg')
    if JQ_MODE:
        text = '[CQ:image,file=' + file_id + '.jpg]'
    else:
        text = '[ 图片, 请点击查看' + pic_url + ' ]'
    if update.message.caption:
        text += update.message.caption

        cq_send(update, text, qq_group_id)
    elif update.edited_message:
        tg_group_id = update.edited_message.chat_id  # telegram group id
        qq_group_id, _, forward_index = get_forward_index(tg_group_id=int(tg_group_id))

        file_id = update.edited_message.photo[-1].file_id
        pic_url = tg_get_pic_url(file_id, 'jpg')
        if JQ_MODE:
            text = '[CQ:image,file=' + file_id + '.jpg]'
        else:
            text = '[图片, 请点击查看' + pic_url + ' ]'
        if update.edited_message.caption:
            text += update.edited_message.caption
        cq_send(update, text, qq_group_id, edited=True)
コード例 #2
0
def sticker_from_telegram(bot: telegram.Bot, update: telegram.Update):

    message: telegram.Message = update.effective_message

    tg_group_id = message.chat_id  # telegram group id
    forward_index = get_forward_index(tg_group_id=tg_group_id)

    reply_entity = list()

    file_id = message.sticker.file_id
    if global_vars.JQ_MODE:
        tg_get_pic_url(file_id, 'png')
        reply_entity.append({
            'type': 'image',
            'data': {'file': file_id + '.png'}
        })
    elif IMAGE_LINK_MODE[forward_index]:
        pic_url = tg_get_pic_url(file_id, 'png')
        reply_entity.append({
            'type': 'text',
            'data': {'text': '[ ' + message.sticker.emoji + ' sticker, 请点击查看' + pic_url + ' ]'}
        })
    else:
        reply_entity.append({
            'type': 'text',
            'data': {'text': '[ ' + message.sticker.emoji + ' sticker ]'}
        })
    qq_message_id = send_from_tg_to_qq(forward_index,
                                       message=reply_entity,
                                       tg_group_id=tg_group_id,
                                       tg_user=update.effective_user,
                                       tg_forward_from=message,
                                       tg_reply_to=message.reply_to_message)
    global_vars.mdb.append_message(qq_message_id, message.message_id, forward_index, 0)
コード例 #3
0
def document_from_telegram(bot: telegram.Bot,
                           update: telegram.Update):
    """
    if update.message:
        message: telegram.Message = update.message
        edited = False
    else:
        message: telegram.Message = update.edited_message
        edited = True
    """

    message: telegram.Message = update.effective_message
    edited = (bool(getattr(update, "edited_message", None)) or bool(getattr(update, "edited_channel_post", None)))

    tg_group_id = message.chat_id  # telegram group id
    forward_index = get_forward_index(tg_group_id=tg_group_id)

    reply_entity = list()

    reply_entity.append({
        'type': 'text',
        'data': {'text': '[ 文件 ]'}
    })
    qq_message_id = send_from_tg_to_qq(forward_index,
                                       message=reply_entity,
                                       tg_group_id=tg_group_id,
                                       tg_user=update.effective_user,
                                       tg_forward_from=message,
                                       tg_reply_to=message.reply_to_message,
                                       edited=edited)
    global_vars.mdb.append_message(qq_message_id, message.message_id, forward_index, 0)
コード例 #4
0
def qq_drive_mode(message):
    qq_group_id = int(message.group)
    _, tg_group_id, forward_index = get_forward_index(qq_group_id=qq_group_id)

    if DRIVE_MODE[forward_index]:
        return True
    return False
コード例 #5
0
def tg_drive_mode(bot, update):
    tg_group_id = update.message.chat_id  # telegram group id
    qq_group_id, _, forward_index = get_forward_index(
        tg_group_id=int(tg_group_id))

    if DRIVE_MODE[forward_index]:
        raise DispatcherHandlerStop()
    if update.message.forward_from_chat:
        if update.message.forward_from_chat.type == 'channel':
            if update.message.forward_from_chat.id in filter_list['channels']:
                drive_mode_on(forward_index, tg_group_id,
                              update.message.from_user, qq_group_id, 0)
                raise DispatcherHandlerStop()

    for keyword in filter_list['keywords']:
        if update.message.caption:
            if keyword in update.message.caption:
                drive_mode_on(forward_index, tg_group_id,
                              update.message.from_user, qq_group_id, 0)
                raise DispatcherHandlerStop()
        elif update.message.text:
            if keyword in update.message.text:
                drive_mode_on(forward_index, tg_group_id,
                              update.message.from_user, qq_group_id, 0)
                raise DispatcherHandlerStop()
コード例 #6
0
def qq_drive_mode(message):
    qq_group_id = int(message.group)
    text = message.text  # get message text
    text, _ = re.subn('&', '&', text)  # restore characters
    text, _ = re.subn('[', '[', text)
    text, _ = re.subn(']', ']', text)
    text, _ = re.subn(',', ',', text)

    for command in global_vars.command_list:  # process all non-forward commands
        if command.qq_only:
            if text == command.command:
                command.handler(qq_group_id, int(message.qq))
                return True

    _, tg_group_id, forward_index = get_forward_index(qq_group_id=qq_group_id)
    if forward_index == -1:
        return True

    for command in global_vars.command_list:  # process all forward commands
        if not command.tg_only and not command.qq_only:
            if text == command.command:
                command.handler(forward_index, tg_group_id, None, qq_group_id, int(message.qq))
                return True

    return False
コード例 #7
0
def test(message):
    qq_group_id = int(message.group)
    _, tg_group_id, forward_index = get_forward_index(qq_group_id=qq_group_id)
    if 'Test_mode' in FORWARD_LIST[forward_index] and FORWARD_LIST[
            forward_index]['Test_mode']:
        logging.info(message)
    return False
コード例 #8
0
def handle_group_increase(context):
    qq_group_id = context.get('group_id')
    sub_type = context.get('sub_type')
    user_id = context.get('user_id')
    operator_id = context.get('operator_id')
    logger.debug(context)

    forward_index = get_forward_index(qq_group_id=qq_group_id)
    if forward_index == -1:
        return ''

    # reload namelist because there is a new member here
    global_vars.reload_qq_namelist(forward_index)

    qq_name = get_qq_name_encoded(user_id, forward_index)
    operator_name = get_qq_name_encoded(operator_id, forward_index)

    if sub_type == 'approve':
        result = f'{qq_name} approved by {operator_name} joined the group.'
    else:
        result = f'{qq_name} invited by {operator_name} joined the group.'

    global_vars.tg_bot.sendMessage(chat_id=FORWARD_LIST[forward_index]['TG'],
                                   text=result,
                                   parse_mode='HTML')

    return ''
コード例 #9
0
def handle_group_decrease(context):
    qq_group_id = context.get('group_id')
    sub_type = context.get('sub_type')
    user_id = context.get('user_id')
    operator_id = context.get('operator_id')
    logger.debug(context)

    forward_index = get_forward_index(qq_group_id=qq_group_id)
    if forward_index == -1:
        return ''

    if sub_type == 'leave':
        if str(user_id) == str(QQ_BOT_ID):
            result = 'Your bot left the group.'
        else:
            qq_name = get_qq_name_encoded(user_id, forward_index)
            result = f'{qq_name} left the group.'
    elif sub_type == 'kick':
        qq_name = get_qq_name_encoded(user_id, forward_index)
        operator_name = get_qq_name_encoded(operator_id, forward_index)
        result = f'{qq_name} was kicked by {operator_name}.'
    else:
        operator_name = get_qq_name_encoded(operator_id, forward_index)
        result = f'Your bot was kicked by {operator_name}.'

    global_vars.tg_bot.sendMessage(chat_id=FORWARD_LIST[forward_index]['TG'],
                                   text=result,
                                   parse_mode='HTML')
    return ''
コード例 #10
0
def text_from_telegram(bot: telegram.Bot, update: telegram.Update):
    if update.message:
        message: telegram.Message = update.message
        edited = False
    else:
        message: telegram.Message = update.edited_message
        edited = True

    tg_group_id = message.chat_id  # telegram group id
    forward_index = get_forward_index(tg_group_id=tg_group_id)

    if edited:
        recall_message(forward_index, message)

    if message.text.startswith('//'):
        return

    reply_entity = list()

    reply_entity.append({'type': 'text', 'data': {'text': message.text}})
    qq_message_id = send_from_tg_to_qq(forward_index,
                                       message=reply_entity,
                                       tg_group_id=tg_group_id,
                                       tg_user=message.from_user,
                                       tg_forward_from=message,
                                       tg_reply_to=message.reply_to_message,
                                       edited=edited)
    global_vars.mdb.append_message(qq_message_id, message.message_id,
                                   forward_index, 0)
コード例 #11
0
ファイル: for_fun.py プロジェクト: xiaofd/coolq-telegram-bot
def send_group_id(tg_group_id, tg_user):
    qq_group_id, _, forward_index = get_forward_index(
        tg_group_id=int(tg_group_id))
    if forward_index == -1:
        raise DispatcherHandlerStop()
    username = get_full_user_name(tg_user)
    msg = username + ': [CQ:dice]'
    global_vars.qq_bot.send(SendGroupMessage(group=qq_group_id, text=msg))
コード例 #12
0
def ignore_old_message(bot, update):  # ignore old message that are more than 60s ago
    tg_group_id = update.message.chat_id  # telegram group id
    if tg_group_id > 0:  # ignore private chat
        raise DispatcherHandlerStop()
    qq_group_id, _, forward_index = get_forward_index(tg_group_id=int(tg_group_id))

    if (datetime.datetime.now() - update.message.date).total_seconds() > 60:
        raise DispatcherHandlerStop()
コード例 #13
0
def tg_drive_mode(bot, update):
    tg_group_id = update.message.chat_id  # telegram group id
    qq_group_id, _, forward_index = get_forward_index(
        tg_group_id=int(tg_group_id))
    # if forward_index == -1:
    #     raise dispatcher.DispatcherHandlerStop()
    if DRIVE_MODE[forward_index]:
        raise DispatcherHandlerStop()
コード例 #14
0
def test(message: RcvdGroupMessage):
    qq_group_id = int(message.group)
    _, tg_group_id, forward_index = get_forward_index(qq_group_id=qq_group_id)
    if 'Test_mode' in FORWARD_LIST[forward_index] and FORWARD_LIST[
            forward_index]['Test_mode']:
        logging.info(message)
    # if message.from_anonymous:
    #     logging.info(CQAnonymousInfo(base64.b64decode(message.from_anonymous)))
    return False
コード例 #15
0
def photo_from_telegram(bot: telegram.Bot, update: telegram.Update):
    if update.message:
        message: telegram.Message = update.message
        edited = False
    else:
        message: telegram.Message = update.edited_message
        edited = True

    tg_group_id = message.chat_id  # telegram group id
    forward_index = get_forward_index(tg_group_id=tg_group_id)

    if edited:
        recall_message(forward_index, message)

    reply_entity = list()

    file_id = message.photo[-1].file_id
    pic_url = tg_get_pic_url(file_id, 'jpg')
    if JQ_MODE:
        reply_entity.append({
            'type': 'image',
            'data': {
                'file': file_id + '.jpg'
            }
        })
        if message.caption:
            reply_entity.append({
                'type': 'text',
                'data': {
                    'text': message.caption
                }
            })
    else:
        if message.caption:
            reply_entity.append({
                'type': 'text',
                'data': {
                    'text': '[ 图片, 请点击查看' + pic_url + ' ]' + message.caption
                }
            })
        else:
            reply_entity.append({
                'type': 'text',
                'data': {
                    'text': '[ 图片, 请点击查看' + pic_url + ' ]'
                }
            })
    qq_message_id = send_from_tg_to_qq(forward_index,
                                       message=reply_entity,
                                       tg_group_id=tg_group_id,
                                       tg_user=message.from_user,
                                       tg_forward_from=message,
                                       tg_reply_to=message.reply_to_message,
                                       edited=edited)
    global_vars.mdb.append_message(qq_message_id, message.message_id,
                                   forward_index, 0)
コード例 #16
0
def qq_drive_mode(context: dict):
    qq_group_id = context.get('group_id')
    qq_discuss_id = context.get('discuss_id')

    forward_index = get_forward_index(qq_group_id=qq_group_id,
                                      qq_discuss_id=qq_discuss_id)

    if global_vars.DRIVE_MODE[forward_index]:  # normal block
        return ''
    return {'pass': True}
コード例 #17
0
def text_from_telegram(bot, update):
    if update.message:
        tg_group_id = update.message.chat_id  # telegram group id
        qq_group_id, _, forward_index = get_forward_index(tg_group_id=int(tg_group_id))

        text = update.message.text
        if text.startswith('//'):  # feature, comment will no be send to qq
            return
        else:
            cq_send(update, text, qq_group_id)
    elif update.edited_message:
        tg_group_id = update.edited_message.chat_id  # telegram group id
        qq_group_id, _, forward_index = get_forward_index(tg_group_id=int(tg_group_id))

        text = update.edited_message.text
        if text.startswith('//'):  # feature, comment will no be send to qq
            return
        else:
            cq_send(update, text, qq_group_id, edited=True)
コード例 #18
0
def tg_drive_mode(bot: telegram.Bot, update: telegram.Update):
    if update.message:
        message: telegram.Message = update.message
    else:
        message: telegram.Message = update.edited_message

    tg_group_id = message.chat_id  # telegram group id
    forward_index = get_forward_index(tg_group_id=int(tg_group_id))

    if global_vars.DRIVE_MODE[forward_index]:  # normal block
        raise DispatcherHandlerStop()
コード例 #19
0
def handle_group_member_list(message):
    with open(os.path.join(CQ_GROUP_LIST_ROOT,
                           message.path.split('\\')[-1]),
              'r',
              encoding='utf-8') as f:
        data = f.read()
    member_list = get_group_member_list_info(data)
    qq_group_id = member_list[0].GroupID
    _, _, forward_index = get_forward_index(qq_group_id=int(qq_group_id))
    global_vars.set_group_members(member_list, index=forward_index)
    return True
コード例 #20
0
def sticker_from_telegram(bot, update):
    tg_group_id = update.message.chat_id  # telegram group id
    qq_group_id, _, forward_index = get_forward_index(tg_group_id=int(tg_group_id))

    if PIC_LINK_MODE[forward_index]:
        file_id = update.message.sticker.file_id
        pic_url = tg_get_pic_url(file_id, 'png')
        if JQ_MODE:
            text = '[CQ:image,file=' + file_id + '.png]'
        else:
            text = '[ ' + update.message.sticker.emoji + ' sticker, 请点击查看' + pic_url + ' ]'
    else:
        text = '[' + update.message.sticker.emoji + ' sticker]'
    cq_send(update, text, qq_group_id)
コード例 #21
0
ファイル: for_fun.py プロジェクト: zerod4y/coolq-telegram-bot
def dice(tg_group_id: int,
         tg_user: telegram.User,
         tg_message_id: int,
         tg_reply_to: telegram.Message = None):
    forward_index = get_forward_index(tg_group_id=tg_group_id)
    if forward_index == -1:
        return

    reply_entity = list()
    reply_entity.append({'data': {'text': 'threw a dice'}, 'type': 'text'})
    reply_entity.append({'data': {'type': '1'}, 'type': 'dice'})

    send_from_tg_to_qq(forward_index,
                       reply_entity,
                       tg_group_id=tg_group_id,
                       tg_user=tg_user)
コード例 #22
0
def audio_from_telegram(bot: telegram.Bot, update: telegram.Update):
    message: telegram.Message = update.message

    tg_group_id = message.chat_id  # telegram group id
    forward_index = get_forward_index(tg_group_id=tg_group_id)

    reply_entity = list()

    reply_entity.append({'type': 'text', 'data': {'text': '[ 音频 ]'}})
    qq_message_id = send_from_tg_to_qq(forward_index,
                                       message=reply_entity,
                                       tg_group_id=tg_group_id,
                                       tg_user=message.from_user,
                                       tg_forward_from=message,
                                       tg_reply_to=message.reply_to_message)
    global_vars.mdb.append_message(qq_message_id, message.message_id,
                                   forward_index, 0)
コード例 #23
0
def tg_drive_mode(bot: telegram.Bot,
                  update: telegram.Update):
    """
    if update.message:
        message: telegram.Message = update.message
    else:
        message: telegram.Message = update.edited_message
    """
    message: telegram.Message = update.effective_message
    edited = (bool(getattr(update, "edited_message", None)) or bool(getattr(update, "edited_channel_post", None)))

    tg_group_id = message.chat_id  # telegram group id
    forward_index = get_forward_index(tg_group_id=int(tg_group_id))

    if global_vars.DRIVE_MODE[forward_index]:  # normal block
        logger.debug('Telegram message ignored: drive mode is on')
        raise DispatcherHandlerStop()
コード例 #24
0
def tg_command(bot, update: Update):
    tg_group_id = update.message.chat_id  # telegram group id
    for command in global_vars.command_list:  # process all non-forward commands
        if command.tg_only:
            if update.message.text == command.command:
                command.handler(tg_group_id, update.message.from_user)
                raise DispatcherHandlerStop()

    qq_group_id, _, forward_index = get_forward_index(tg_group_id=int(tg_group_id))
    if forward_index == -1:
        raise DispatcherHandlerStop()

    for command in global_vars.command_list:  # process all forward commands
        if not command.tg_only and not command.qq_only:
            if update.message.text == command.command:
                command.handler(forward_index, tg_group_id, update.message.from_user, qq_group_id, 1)
                raise DispatcherHandlerStop()
コード例 #25
0
def link_from_telegram(bot, update):
    tg_group_id = update.message.chat_id  # telegram group id
    qq_group_id, _, forward_index = get_forward_index(
        tg_group_id=int(tg_group_id))
    link_regex = re.compile(
        r'^https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]$')
    text = update.message.text
    if link_regex.match(text):  # feature, comment will no be send to qq
        article = Article(text)
        article.download()
        article.parse()
        sender_name = trim_emoji(
            get_full_user_name(update.message.from_user)
        ) + ':'  # unicode emoji cannot pass into create_cq_share
        msg = create_cq_share(text, sender_name, article.title,
                              article.top_image if article.top_image else '')
        cq_send(update, msg, qq_group_id)
        raise DispatcherHandlerStop()
コード例 #26
0
ファイル: for_fun.py プロジェクト: zerod4y/coolq-telegram-bot
def rps(tg_group_id: int, tg_user: telegram.User, tg_message_id: int,
        tg_reply_to: telegram.Message):
    forward_index = get_forward_index(tg_group_id=tg_group_id)
    if forward_index == -1:
        return

    reply_entity = list()
    reply_entity.append({
        'data': {
            'text': 'played rock–paper–scissors'
        },
        'type': 'text'
    })
    reply_entity.append({'data': {'type': '1'}, 'type': 'rps'})

    send_from_tg_to_qq(forward_index,
                       reply_entity,
                       tg_group_id=tg_group_id,
                       tg_user=tg_user)
コード例 #27
0
def handle_forward(context):
    qq_group_id = context.get('group_id')
    qq_discuss_id = context.get('discuss_id')

    forward_index = get_forward_index(qq_group_id=qq_group_id,
                                      qq_discuss_id=qq_discuss_id)

    tg_message_id_list = send_from_qq_to_tg(forward_index,
                                            message=context['message'],
                                            qq_group_id=qq_group_id,
                                            qq_discuss_id=qq_discuss_id,
                                            qq_user=context['user_id'])

    # save message to database, using telegram message id as index
    for msg_id in tg_message_id_list:
        global_vars.mdb.append_message(context.get('message_id'), msg_id,
                                       forward_index, context.get('user_id'))

    return ''
コード例 #28
0
def handle_group_upload(context):
    qq_group_id = context.get('group_id')
    user_id = context.get('user_id')
    file = context.get('file')

    logger.debug(context)

    forward_index = get_forward_index(qq_group_id=qq_group_id)
    if not forward_index:
        return ''

    qq_name = get_qq_name_encoded(user_id, forward_index)

    result = f'<b>{qq_name}</b> sent a 📎group file: {file["name"]}. Please view it on QQ.'
    global_vars.tg_bot.sendMessage(chat_id=FORWARD_LIST[forward_index]['TG'],
                                   text=result,
                                   parse_mode='HTML')

    return ''
コード例 #29
0
def tg_command(bot: telegram.Bot, update: telegram.Update):
    if update.edited_message:  # handle edit
        message: telegram.Message = update.edited_message
    else:
        message: telegram.Message = update.message

    if not message.text.startswith('!!'):  # no command indicator
        return

    tg_group_id = message.chat_id  # telegram group id
    tg_reply_to = message.reply_to_message

    logger.debug('Command indicator met: ' + message.text)
    text = message.text[2:]

    for command in global_vars.command_list:  # process all non-forward commands
        if command.tg_only and (text == command.command
                                or text == command.short_command):
            logger.debug(f'Matched Telegram only command: {command.command}')
            command.handler(tg_group_id=tg_group_id,
                            tg_user=message.from_user,
                            tg_message_id=message.message_id,
                            tg_reply_to=tg_reply_to)

            raise DispatcherHandlerStop()

    forward_index = get_forward_index(tg_group_id=tg_group_id)
    if forward_index == -1:
        logger.warning(
            'Forward not found, please check your forward settings.')
        raise DispatcherHandlerStop()

    for command in global_vars.command_list:  # process all forward commands
        if not command.tg_only and not command.qq_only and (
                text == command.command or text == command.short_command):
            logger.debug(f'Matched general command: {command.command}')
            command.handler(forward_index,
                            tg_user=message.from_user,
                            tg_group_id=tg_group_id,
                            tg_message_id=message.message_id,
                            tg_reply_to=tg_reply_to)
            raise DispatcherHandlerStop()
コード例 #30
0
def qq_command(context):
    if len(context['message']) > 1:  # rich text can not be commands
        return {'pass': True}

    if context['message'][0][
            'type'] != 'text':  # commands can only be pure text
        return {'pass': True}

    qq_group_id = context.get('group_id')
    qq_discuss_id = context.get('discuss_id')
    text = context['message'][0]['data']['text']  # get message text

    if not text.startswith('!!'):  # no command indicator
        return {'pass': True}

    logger.debug('Command indicator met: ' + text)
    text = text[2:]

    for command in global_vars.command_list:  # process all non-forward commands
        if command.qq_only and (text == command.command
                                or text == command.short_command):
            logger.debug(f'Matched QQ only command: {command.command}')
            return command.handler(qq_group_id, qq_discuss_id,
                                   int(context['user_id']))

    forward_index = get_forward_index(qq_group_id=qq_group_id,
                                      qq_discuss_id=qq_discuss_id)
    if forward_index == -1:
        logger.warning(
            'Forward not found, please check your forward settings.')
        return ''

    for command in global_vars.command_list:  # process all forward commands
        if not command.tg_only and not command.qq_only and (
                text == command.command or text == command.short_command):
            logger.debug(f'Matched general command: {command.command}')
            return command.handler(forward_index,
                                   qq_group_id=qq_group_id,
                                   qq_discuss_id=qq_discuss_id,
                                   qq_user=int(context['user_id']))

    return {'pass': True}