Extend JSON message support and add a choice to forward voices

This commit is contained in:
cgluWxh 2022-01-25 10:14:48 +08:00
parent 7f37553ad8
commit 481d911887
2 changed files with 32 additions and 0 deletions

View File

@ -9,6 +9,7 @@ from ehforwarderbot.message import LocationAttribute, LinkAttribute, Substitutio
from . import GoCQHttp
from .Utils import cq_get_image, download_voice, download_file
# from .Utils import convert_voice
class QQMsgProcessor:
@ -63,6 +64,23 @@ class QQMsgProcessor:
efb_msg.text = self._('[Voice Message] Please check it on your QQ')
logging.getLogger(__name__).exception("Failed to download voice")
return [efb_msg]
# def qq_record_wrapper(self, data, chat: Chat = None): # A choice if you need to receive voice messages via Telegram
# efb_msg = Message()
# try:
# file_path = "## THE PATH OF YOUR GO-CQHTTP VOICE FOLDER LIKE /home/efb/cq/data/voices/ ##"+data["file"]
# efb_msg.type = MsgType.Audio
# efb_msg.file = convert_voice(file_path)
# mime = magic.from_file(efb_msg.file.name, mime=True)
# if isinstance(mime, bytes):
# mime = mime.decode()
# efb_msg.path = efb_msg.file.name
# efb_msg.mime = mime
# except Exception:
# efb_msg.type = MsgType.Unsupported
# efb_msg.text = self._('[Voice Message] Please check it on your QQ')
# logging.getLogger(__name__).exception("Failed to download voice")
# return [efb_msg]
def qq_share_wrapper(self, data, chat: Chat = None):
efb_msg = Message(
@ -289,6 +307,12 @@ class QQMsgProcessor:
meta_view = dict_data['meta'][dict_data['view']]
efb_msg.text = "{prompt}\n\n{desc}\n\n{url}\n\n{preview}".format(prompt=dict_data['prompt'], desc=meta_view['desc'], url=meta_view['jumpUrl'], preview=meta_view['preview'])
elif dict_data['app'] == 'com.tencent.map':
efb_msg.text = "【位置消息】\n地址:{}\n点击导航高德https://urljump.vercel.app/?query=amapuri://route/plan?dev=0&dlat={}&dlon={}".format(dict_data['meta']['Location.Search']['address'],dict_data['meta']['Location.Search']['lat'],dict_data['meta']['Location.Search']['lng'])
elif dict_data['app'] == 'com.tencent.qq.checkin':
efb_msg.text = "【群签到】\n内容:{}\n图片:{}".format(dict_data['meta']['checkInData']['desc'],dict_data['meta']['checkInData']['cover']['url'])
except:
self.logger.error(f"json_wrapper_info: {data}\nexc_info:{sys.exc_info()[0]}")

View File

@ -1,6 +1,7 @@
import logging
import tempfile
import urllib.request
# import os
from gettext import translation
from urllib.error import URLError, HTTPError, ContentTooShortError
@ -765,3 +766,10 @@ def download_voice(filename: str, api_root: str, access_token: str):
raise EOFError('File downloaded is Empty')
file.seek(0)
return file
# def convert_voice(filename: str): # A weak function to convert silk to mp3. Plz install ffmpeg and Silkdecoder(from https://github.com/kn007/silk-v3-decoder) first.
# os.system("silkdecoder {} {}.pcm".format(filename,filename))
# os.system("ffmpeg -y -f s16le -ar 24000 -ac 1 -i {}.pcm {}.mp3".format(filename,filename))
# os.system("rm -rf {} {}.pcm".format(filename,filename))
# file=open("{}.mp3".format(filename),"rb")
# return file