diff --git a/commands/core.py b/commands/core.py index a7852d53..d56b7bac 100644 --- a/commands/core.py +++ b/commands/core.py @@ -2,6 +2,7 @@ import os import requests from command import CommandRegistry +from little_shit import get_source from apiclient import client as api __registry__ = cr = CommandRegistry() @@ -20,12 +21,9 @@ def tuling123(args_text, ctx_msg, internal=False): url = 'http://www.tuling123.com/openapi/api' data = { 'key': os.environ.get('TURING123_API_KEY'), - 'info': args_text + 'info': args_text, + 'userid': get_source(ctx_msg) } - if ctx_msg.get('sender_uid'): - data['userid'] = ctx_msg.get('sender_uid') - elif ctx_msg.get('sender_id'): - data['userid'] = ctx_msg.get('sender_id').strip('@')[-32:] resp = requests.post(url, data=data) if resp.status_code == 200: json = resp.json() diff --git a/filters/speech_recognition_90.py b/filters/speech_recognition_90.py index cc67e018..828551cb 100644 --- a/filters/speech_recognition_90.py +++ b/filters/speech_recognition_90.py @@ -13,6 +13,7 @@ import speech_recognition as sr from filter import as_filter from commands import core +from little_shit import get_source def _recognize_baidu(wav_path, unique_id, api_key, secret_key, language='zh'): @@ -74,7 +75,7 @@ def _filter(ctx_msg): service_full_name = '百度语音识别' text = _recognize_baidu( wav_path, - ctx_msg.get('sender_id')[-60:], + get_source(ctx_msg), os.environ.get('BAIDU_SPEECH_API_KEY'), os.environ.get('BAIDU_SPEECH_SECRET_KEY'), language='zh' diff --git a/little_shit.py b/little_shit.py index a259387a..ff3c7a69 100644 --- a/little_shit.py +++ b/little_shit.py @@ -1,4 +1,5 @@ import os +import hashlib import random from datetime import datetime @@ -47,21 +48,24 @@ def get_source(ctx_msg): Source is used to distinguish the interactive sessions. Note: This value may change after restarting the bot. - :return: an unique value representing a source, or a random value if something strange happened + :return: a 32 character unique string (md5) representing a source, or a random value if something strange happened """ + source = None if ctx_msg.get('via') == 'qq': if ctx_msg.get('type') == 'group_message' and ctx_msg.get('group_uid') and ctx_msg.get('sender_uid'): - return 'g' + ctx_msg.get('group_uid') + 'p' + ctx_msg.get('sender_uid') + source = 'g' + ctx_msg.get('group_uid') + 'p' + ctx_msg.get('sender_uid') elif ctx_msg.get('type') == 'discuss_message' and ctx_msg.get('discuss_id') and ctx_msg.get('sender_uid'): - return 'd' + ctx_msg.get('discuss_id') + 'p' + ctx_msg.get('sender_uid') + source = 'd' + ctx_msg.get('discuss_id') + 'p' + ctx_msg.get('sender_uid') elif ctx_msg.get('type') == 'friend_message' and ctx_msg.get('sender_uid'): - return 'p' + ctx_msg.get('sender_uid') + source = 'p' + ctx_msg.get('sender_uid') elif ctx_msg.get('via') == 'wx': if ctx_msg.get('type') == 'group_message' and ctx_msg.get('group_id') and ctx_msg.get('sender_id'): - return 'g' + ctx_msg.get('group_id') + 'p' + ctx_msg.get('sender_id') + source = 'g' + ctx_msg.get('group_id') + 'p' + ctx_msg.get('sender_id') elif ctx_msg.get('type') == 'friend_message' and ctx_msg.get('sender_id'): - return 'p' + ctx_msg.get('sender_id') - return str(int(datetime.now().timestamp())) + str(random.randint(100, 999)) + source = 'p' + ctx_msg.get('sender_id') + if not source: + source = str(int(datetime.now().timestamp())) + str(random.randint(100, 999)) + return hashlib.md5(source.encode('utf-8')).hexdigest() def get_target(ctx_msg): @@ -69,7 +73,8 @@ def get_target(ctx_msg): Target is used to distinguish the records in database. Note: This value will not change after restarting the bot. - :return: an unique value representing a target, or None if there is no persistent unique value + :return: an unique string (account id with some flags) representing a target, + or None if there is no persistent unique value """ if ctx_msg.get('via') == 'qq': if ctx_msg.get('type') == 'group_message' and ctx_msg.get('group_uid'):