def extract_cq_rich(message): """ get rich text parameters :param message: raw cq message :return: url, text """ result = cq_rich_regex.findall(message)[0] return decode_cq_escape(result[0]), decode_cq_escape(result[1])
def extract_cq_record(message): """ get record parameters :param message: raw cq message :return: file, magic """ result = cq_record_regex.findall(message)[0] return decode_cq_escape(result[0]), decode_cq_escape(result[1])
def extract_cq_music(message): """ get music parameters :param message: raw cq message :return: type, id """ result = cq_music_regex.findall(message)[0] return decode_cq_escape(result[0]), decode_cq_escape(result[1])
def extract_cq_share(message): """ get share parameters :param message: raw cq message :return: url, title, content, image_url """ result = cq_share_regex.findall(message)[0] return decode_cq_escape(result[0]), decode_cq_escape(result[1]), \ decode_cq_escape(result[2]), decode_cq_escape(result[3])
def extract_cq_custom_music(message): """ get custom music patameters :param message: :return: url, audio, title, content, image """ result = cq_custom_music_regex.findall(message)[0] return decode_cq_escape(result[0]), decode_cq_escape(result[1]), \ decode_cq_escape(result[2]), decode_cq_escape(result[3]), \ decode_cq_escape(result[4])
def __init__(self, data, is_base64=True): data = base64.decodebytes(data.encode()) if is_base64 else data info = CQUnpack(data) self.QQID = info.get_long() self.Nickname = decode_cq_escape(info.get_length_str().decode('gb18030')) self.Sex = info.get_int() self.Age = info.get_int()
def extract_cq_rps(message): """ get rps parameters :param message: raw cq message :return: rps number(stone: 1, scissors: 2, paper: 3) """ result = cq_rps_regex.findall(message)[0] return decode_cq_escape(result[0])
def extract_cq_dice(message): """ get dice parameters :param message: raw cq message :return: dice number """ result = cq_dice_regex.findall(message)[0] return decode_cq_escape(result[0])
def __init__(self, data, is_base64=True): data = base64.decodebytes(data.encode()) if is_base64 else data info = CQUnpack(data) self.GroupID = info.get_long() self.QQID = info.get_long() self.Nickname = decode_cq_escape( info.get_length_str().decode('gb18030')) self.Card = decode_cq_escape(info.get_length_str().decode('gb18030')) self.Sex = info.get_int() self.Age = info.get_int() self.Address = info.get_length_str().decode('gb18030') self.JoinGroupTime = info.get_int() self.LastSpeakTime = info.get_int() self.LevelName = info.get_length_str().decode('gb18030') self.Authority = info.get_int() self.IsGroupAdmin = self.Authority in [2, 3] self.IsGroupOwner = self.Authority in [3] self.IsBad = (info.get_int() == 1) self.SpecialTitle = decode_cq_escape( info.get_length_str().decode('gb18030')) self.SpecialTitleExpiredTime = info.get_int() self.IsAllowedToModifyCard = (info.get_int() == 1)
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('<', '<').replace('>', '>') 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('<', '<').replace('>', '>') 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