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 """ 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) if forward_index == -1: 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=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)
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)
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)
def video_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) if edited: recall_message(forward_index, message) # don't forward this message if (message.caption and message.caption.startswith('//')) or ( message.reply_to_message and message.reply_to_message.caption and message.reply_to_message.caption.startswith('//')) or ( message.reply_to_message and message.reply_to_message.text and message.reply_to_message.text.startswith('//')): logger.debug('Message ignored: matched comment pattern') raise DispatcherHandlerStop() reply_entity = list() reply_entity.append({'type': 'text', 'data': {'text': '[ 视频 ]'}}) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': message.caption } }) 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)
def audio_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() 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) global_vars.mdb.append_message(qq_message_id, message.message_id, forward_index, 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', False) 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', True) 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)
def location_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) latitude = message.location.latitude longitude = message.location.longitude reply_entity = list() reply_entity.append({ 'type': 'text', 'data': { 'text': '分享了一个位置:' + get_location_from_baidu(latitude, longitude) } }) 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)
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) if edited: recall_message(forward_index, message) # don't forward this message if (message.caption and message.caption.startswith('//')) or ( message.reply_to_message and message.reply_to_message.caption and message.reply_to_message.caption.startswith('//')) or ( message.reply_to_message and message.reply_to_message.text and message.reply_to_message.text.startswith('//')): logger.debug('Message ignored: matched comment pattern') raise DispatcherHandlerStop() reply_entity = list() if message.document.mime_type == 'video/mp4': file_id = message.document.file_id if global_vars.JQ_MODE: tg_get_pic_url(file_id, 'gif', False) if os.path.getsize(os.path.join(CQ_IMAGE_ROOT, file_id + '.gif')) > 5242880: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 视频:大小超过 5 MB ]' } }) else: reply_entity.append({ 'type': 'image', 'data': { 'file': file_id + '.gif' } }) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': message.caption } }) elif IMAGE_LINK_MODE[forward_index]: pic_url = tg_get_pic_url(file_id, 'gif', True) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 视频, 请点击查看' + pic_url + ' ]' + message.caption } }) else: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 视频, 请点击查看' + pic_url + ' ]' } }) else: reply_entity.append({'type': 'text', 'data': {'text': '[ 视频 ]'}}) elif message.document.mime_type == 'image/gif': file_id = message.document.file_id if global_vars.JQ_MODE: file = global_vars.tg_bot.getFile(file_id) file.download(custom_path=os.path.join(CQ_IMAGE_ROOT, file_id + '.gif')) reply_entity.append({ 'type': 'image', 'data': { 'file': file_id + '.gif' } }) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': message.caption } }) elif IMAGE_LINK_MODE[forward_index]: file = global_vars.tg_bot.getFile(file_id) file.download(custom_path=os.path.join(CQ_IMAGE_ROOT, file_id + '.gif')) pic_url = get_short_url(SERVER_PIC_URL + file_id + '.gif') if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ GIF, 请点击查看' + pic_url + ' ]' + message.caption } }) else: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ GIF, 请点击查看' + pic_url + ' ]' } }) else: reply_entity.append({'type': 'text', 'data': {'text': '[ GIF ]'}}) else: 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)
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 """ 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) if edited: recall_message(forward_index, message) # don't forward this message if (message.caption and message.caption.startswith('//')) or ( message.reply_to_message and message.reply_to_message.caption and message.reply_to_message.caption.startswith('//')) or ( message.reply_to_message and message.reply_to_message.text and message.reply_to_message.text.startswith('//')): logger.debug('Message ignored: matched comment pattern') raise DispatcherHandlerStop() reply_entity = list() file_id = message.photo[-1].file_id if global_vars.JQ_MODE: tg_get_pic_url(file_id, 'jpg', False) reply_entity.append({ 'type': 'image', 'data': { 'file': file_id + '.jpg' } }) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': message.caption } }) elif IMAGE_LINK_MODE[forward_index]: pic_url = tg_get_pic_url(file_id, 'jpg', True) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 图片, 请点击查看' + pic_url + ' ]' + message.caption } }) else: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 图片, 请点击查看' + pic_url + ' ]' } }) else: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 图片 ]' + message.caption } }) 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)
def photo_from_telegram(bot: telegram.Bot, update: telegram.Update): """ handle photos sent from telegram :param bot: :param update: :return: """ 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() photo = message.photo[-1] file_id = photo.file_id if global_vars.JQ_MODE: filename = tg_get_file(file_id) reply_entity.append({'type': 'image', 'data': {'file': filename}}) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': message.caption } }) elif IMAGE_LINK_MODE[forward_index]: filename = tg_get_file(file_id) pic_url = get_url(filename) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 图片, 请点击查看' + pic_url + ' ]' + message.caption } }) else: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 图片, 请点击查看' + pic_url + ' ]' } }) else: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 图片 ]' + message.caption } }) 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)
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() file_id = message.document.file_id if message.document.mime_type == 'video/mp4': file_id = message.document.file_id if global_vars.JQ_MODE: filename = tg_get_file(file_id, mp4=True) if os.path.getsize(os.path.join(CQ_IMAGE_ROOT, filename)) > 5242880: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 视频:大小超过 5 MB ]' } }) else: reply_entity.append({ 'type': 'image', 'data': { 'file': filename } }) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': message.caption } }) elif IMAGE_LINK_MODE[forward_index]: filename = tg_get_file(file_id, mp4=True) pic_url = get_url(filename) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 视频, 请点击查看' + pic_url + ' ]' + message.caption } }) else: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ 视频, 请点击查看' + pic_url + ' ]' } }) else: reply_entity.append({'type': 'text', 'data': {'text': '[ 视频 ]'}}) elif message.document.mime_type == 'image/gif': if global_vars.JQ_MODE: filename = tg_get_file(file_id) reply_entity.append({'type': 'image', 'data': {'file': filename}}) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': message.caption } }) elif IMAGE_LINK_MODE[forward_index]: filename = tg_get_file(file_id) pic_url = get_url(filename) if message.caption: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ GIF, 请点击查看' + pic_url + ' ]' + message.caption } }) else: reply_entity.append({ 'type': 'text', 'data': { 'text': '[ GIF, 请点击查看' + pic_url + ' ]' } }) else: reply_entity.append({'type': 'text', 'data': {'text': '[ GIF ]'}}) else: 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)