Esempio n. 1
0
 def replace_name(qq_number):  # replace each qq number with preset id
     qq_number = qq_number.group(1)
     if int(qq_number) == QQ_BOT_ID:
         return '@bot'
     result = '@' + get_qq_name(int(qq_number), forward_index)
     result = result.replace(':', ' ')
     return result
def new(message):
    qq_group_id = int(message.group)
    _, tg_group_id, forward_index = get_forward_index(qq_group_id=qq_group_id)
    if cq_regex.match(message.text):
        text = ''
        if cq_dice_regex.match(message.text):
            dice = extract_cq_dice(message.text)
            text = '掷出了 <b>' + str(dice) + '</b>'
        elif cq_shake_regex.match(message.text):
            text = '发送了一个抖动'
        elif cq_rps_regex.match(message.text):
            rps = extract_cq_rps(message.text)
            text = '出了 <b>' + {'1': '石头', '2': '剪刀', '3': '布'}[rps] + '</b>'
        elif cq_rich_regex.match(message.text):
            url, _text = extract_cq_rich(message.text)
            if url:
                text = '<a href="' + url + '">' + _text + '</a>'
            else:
                text = _text
        elif cq_share_regex.match(message.text):
            url, title, content, image_url = extract_cq_share(message.text)
            text = '分享了<a href="' + url + '">' + title + '</a>'
        elif cq_custom_music_regex.match(message.text):
            url, audio, title, content, image = extract_cq_custom_music(
                message.text)
            text = '分享了<a href="' + url + '">' + title + '</a>'
        elif cq_music_regex.match(message.text):
            _type, _id = extract_cq_music(message.text)
            text = '分享了<a href="https://y.qq.com/n/yqq/song/' + +str(
                _id) + + '_num.html"> qq 音乐</a>'
        elif cq_record_regex.match(message.text):
            file, magic = extract_cq_record(message.text)
            text = '说了句话,懒得转了'
        if text:
            full_msg = '<b>' + get_qq_name(int(
                message.qq), forward_index) + '</b>: ' + text.strip()
            global_vars.tg_bot.sendMessage(tg_group_id,
                                           full_msg,
                                           parse_mode='HTML')
            return True
    return False
Esempio n. 3
0
def new(message):
    # logging.info('(' + message.qq + '): ' + message.text)

    qq_group_id = int(message.group)
    _, tg_group_id, forward_index = get_forward_index(qq_group_id=qq_group_id)

    text = message.text  # get message text

    text, _ = cq_image_regex.subn('', text)   # clear CQ:image in text

    # replace special characters
    decode_cq_escape(text)

    text = cq_emoji_regex.sub(lambda x: chr(int(x.group(1))), text)  # replace [CQ:emoji,id=*]
    text = cq_face_regex.sub(lambda x: qq_emoji_list[int(x.group(1))] if int(x.group(1)) in qq_emoji_list else '\u2753', text)  # replace [CQ:face,id=*]
    text = cq_bface_regex.sub('\u2753', text)  # replace bface to '?'
    text = cq_sface_regex.sub(lambda x: qq_sface_list[int(x.group(1)) & 255] if int(x.group(1)) > 100000 and int(x.group(1)) & 255 in qq_sface_list else '\u2753', text)  # replace [CQ:sface,id=*], https://cqp.cc/t/26206

    def replace_name(qq_number):  # replace each qq number with preset id
        qq_number = qq_number.group(1)
        if int(qq_number) == QQ_BOT_ID:
            return '@bot'
        result = '@' + get_qq_name(int(qq_number), forward_index)
        result = result.replace(':', ' ')
        return result

    text = CQAt.PATTERN.sub(replace_name, text)  # replace CQAt to @username

    # send pictures to Telegram group
    pic_send_mode = 2
    # mode = 0 -> direct mode: send cqlink to tg server
    # mode = 1 -> (deprecated) download mode: download to local,send local link to tg server
    # mode = 2 -> download mode: download to local, upload from disk to tg server
    message_parts = cq_image_simple_regex.split(message.text)
    message_parts_count = len(message_parts)
    image_num = message_parts_count - 1
    if image_num == 0:
        # send plain text message with bold group member name
        full_msg_bold = '<b>' + get_qq_name(int(message.qq), forward_index) + '</b>: ' + text.strip().replace('<', '&lt;').replace('>', '&gt;')
        global_vars.tg_bot.sendMessage(tg_group_id, full_msg_bold, parse_mode='HTML')
    else:
        if message_parts[0]:
            part_msg_bold = '<b>' + get_qq_name(int(message.qq), forward_index) + '</b>: ' +\
                        '(1/' + str(message_parts_count) + ')' + message_parts[0].strip().replace('<', '&lt;').replace('>', '&gt;')
            global_vars.tg_bot.sendMessage(tg_group_id, part_msg_bold, parse_mode='HTML')
            part_index = 1
        else:
            message_parts.pop(0)
            message_parts_count -= 1
            part_index = 0
        for matches in CQImage.PATTERN.finditer(message.text):
            # replace QQ number to group member name, get full message text
            if message_parts_count == 1:
                part_msg = get_qq_name(int(message.qq), forward_index) + ': ' + message_parts[part_index].strip()
            else:
                part_msg = get_qq_name(int(message.qq), forward_index) + ': ' + '(' + str(part_index+1) + '/' + str(message_parts_count) + ')' + message_parts[part_index].strip()
            part_index += 1
            filename = matches.group(1)
            url = cq_get_pic_url(filename)
            pic = url
            if pic_send_mode == 1:
                cq_download_pic(filename)
                pic = SERVER_PIC_URL + filename
            elif pic_send_mode == 2:
                cq_download_pic(filename)
                pic = open(os.path.join(CQ_IMAGE_ROOT, filename), 'rb')
            # gif pictures send as document
            if filename.lower().endswith('gif'):
                try:
                    global_vars.tg_bot.sendDocument(tg_group_id, pic, caption=part_msg)
                except BadRequest:
                    # when error occurs, download picture and send link instead
                    error(message)
                    traceback.print_exc()
                    if pic_send_mode == 0:
                        cq_download_pic(filename)
                    pic = get_short_url(SERVER_PIC_URL + filename)
                    global_vars.tg_bot.sendMessage(tg_group_id, pic + '\n' + part_msg)

            # jpg/png pictures send as photo
            else:
                try:
                    # the first image in message attach full message text
                    global_vars.tg_bot.sendPhoto(tg_group_id, pic, caption=part_msg)
                except BadRequest:
                    # when error occurs, download picture and send link instead
                    error(message)
                    traceback.print_exc()
                    if pic_send_mode == 0:
                        cq_download_pic(filename)
                    my_url = get_short_url(SERVER_PIC_URL + filename)
                    pic = my_url
                    global_vars.tg_bot.sendMessage(tg_group_id, pic + '\n' + part_msg)
    return True