mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-28 08:41:29 +00:00
Adjust filter structure
This commit is contained in:
@ -7,14 +7,12 @@ from filter import as_filter
|
||||
|
||||
@as_filter(priority=100)
|
||||
def _filter(ctx_msg):
|
||||
if ctx_msg.get('via') == 'wx':
|
||||
msg_format = ctx_msg.get('format')
|
||||
if msg_format != 'text' and ctx_msg.get('type') != 'friend_message':
|
||||
return False
|
||||
if msg_format not in ('text', 'media'):
|
||||
return False
|
||||
if msg_format == 'text':
|
||||
ctx_msg['text'] = ctx_msg.get('content')
|
||||
elif ctx_msg.get('via') == 'qq':
|
||||
msg_format = ctx_msg.get('format')
|
||||
if msg_format != 'text' and ctx_msg.get('type') != 'friend_message':
|
||||
return False
|
||||
if msg_format not in ('text', 'media'):
|
||||
return False
|
||||
if msg_format == 'text':
|
||||
# Directly use the text in content as the 'text'
|
||||
ctx_msg['text'] = ctx_msg.get('content')
|
||||
return True
|
||||
|
@ -4,6 +4,7 @@ This filter recognizes speech in voice message and stores it in 'text' field of
|
||||
|
||||
import re
|
||||
import os
|
||||
import sys
|
||||
import base64
|
||||
|
||||
import requests
|
||||
@ -57,7 +58,7 @@ def _recognize_bing(wav_path, api_key, language='zh-CN'):
|
||||
|
||||
@as_filter(priority=90)
|
||||
def _filter(ctx_msg):
|
||||
if ctx_msg.get('via') == 'wx' and ctx_msg.get('format') == 'media' and ctx_msg.get('media_type') == 'voice':
|
||||
if ctx_msg.get('format') == 'media' and ctx_msg.get('media_type') == 'voice':
|
||||
m = re.match('\[语音\]\(([/_A-Za-z0-9]+\.mp3)\)', ctx_msg.get('content'))
|
||||
if m:
|
||||
core.echo('正在识别语音内容,请稍等……', ctx_msg)
|
||||
@ -65,20 +66,31 @@ def _filter(ctx_msg):
|
||||
wav_path = os.path.splitext(mp3_path)[0] + '.wav'
|
||||
voice = AudioSegment.from_mp3(mp3_path)
|
||||
voice.export(wav_path, format='wav')
|
||||
text = _recognize_baidu(
|
||||
wav_path,
|
||||
ctx_msg.get('sender_id')[-60:],
|
||||
os.environ.get('BAIDU_SPEECH_API_KEY'),
|
||||
os.environ.get('BAIDU_SPEECH_SECRET_KEY'),
|
||||
language='zh'
|
||||
)
|
||||
# text = _recognize_bing(
|
||||
# wav_path,
|
||||
# os.environ.get('BING_SPEECH_API_KEY'),
|
||||
# language='zh-CN'
|
||||
# )
|
||||
|
||||
service = os.environ.get('SPEECH_RECOGNITION_SERVICE', '').lower()
|
||||
text = None
|
||||
service_full_name = None
|
||||
if service == 'baidu':
|
||||
service_full_name = '百度语音识别'
|
||||
text = _recognize_baidu(
|
||||
wav_path,
|
||||
ctx_msg.get('sender_id')[-60:],
|
||||
os.environ.get('BAIDU_SPEECH_API_KEY'),
|
||||
os.environ.get('BAIDU_SPEECH_SECRET_KEY'),
|
||||
language='zh'
|
||||
)
|
||||
elif service == 'bing':
|
||||
service_full_name = '必应语音识别'
|
||||
text = _recognize_bing(
|
||||
wav_path,
|
||||
os.environ.get('BING_SPEECH_API_KEY'),
|
||||
language='zh-CN'
|
||||
)
|
||||
else:
|
||||
print('Unknown speech recognition service name.', file=sys.stderr)
|
||||
|
||||
if text:
|
||||
reply = '识别结果(百度语音识别):\n%s\n\n下面将把识别到的内容作为文字消息处理……' % text
|
||||
reply = '识别结果(' + service_full_name + '):\n%s\n\n下面将把识别到的内容作为文字消息处理……' % text
|
||||
ctx_msg['text'] = text
|
||||
ctx_msg['from_voice'] = True
|
||||
else:
|
||||
|
@ -3,6 +3,7 @@ This filter intercepts messages not intended to the bot and removes the beginnin
|
||||
"""
|
||||
|
||||
from filter import as_filter
|
||||
from apiclient import client as api
|
||||
|
||||
|
||||
@as_filter(priority=50)
|
||||
@ -15,7 +16,15 @@ def _split_at_xiaokai(ctx_msg):
|
||||
return False
|
||||
at_me = '@' + my_group_nick
|
||||
if not text.startswith(at_me):
|
||||
return False
|
||||
user_info = api.get_user_info().json()
|
||||
if not user_info:
|
||||
return False
|
||||
my_nick = user_info.get('nick')
|
||||
if not my_nick:
|
||||
return False
|
||||
at_me = '@' + my_nick
|
||||
if not text.startswith(at_me):
|
||||
return False
|
||||
text = text[len(at_me):]
|
||||
else:
|
||||
# Not starts with '@'
|
||||
|
25
filters/unitize_context_message_10000.py
Normal file
25
filters/unitize_context_message_10000.py
Normal file
@ -0,0 +1,25 @@
|
||||
"""
|
||||
This filter unitize context messages from different platform.
|
||||
"""
|
||||
|
||||
from filter import as_filter
|
||||
|
||||
|
||||
@as_filter(priority=10000)
|
||||
def _unitize(ctx_msg):
|
||||
if 'group_uid' in ctx_msg:
|
||||
ctx_msg['group_uid'] = str(ctx_msg['group_uid'])
|
||||
if 'sender_uid' in ctx_msg:
|
||||
ctx_msg['sender_uid'] = str(ctx_msg['sender_uid'])
|
||||
if 'sender_id' in ctx_msg:
|
||||
ctx_msg['sender_id'] = str(ctx_msg['sender_id'])
|
||||
if 'discuss_id' in ctx_msg:
|
||||
ctx_msg['discuss_id'] = str(ctx_msg['discuss_id'])
|
||||
if 'group_id' in ctx_msg:
|
||||
ctx_msg['group_id'] = str(ctx_msg['group_id'])
|
||||
if 'id' in ctx_msg:
|
||||
ctx_msg['id'] = str(ctx_msg['id'])
|
||||
|
||||
if ctx_msg.get('via') == 'qq' and not ctx_msg.get('format'):
|
||||
# All QQ messages that can be received are text
|
||||
ctx_msg['format'] = 'text'
|
Reference in New Issue
Block a user