get_source now returns a 32 characters MD5 hash

This commit is contained in:
Richard Chien 2017-01-11 16:33:30 +08:00
parent 58a1352b19
commit 9108f37412
3 changed files with 18 additions and 14 deletions

View File

@ -2,6 +2,7 @@ import os
import requests import requests
from command import CommandRegistry from command import CommandRegistry
from little_shit import get_source
from apiclient import client as api from apiclient import client as api
__registry__ = cr = CommandRegistry() __registry__ = cr = CommandRegistry()
@ -20,12 +21,9 @@ def tuling123(args_text, ctx_msg, internal=False):
url = 'http://www.tuling123.com/openapi/api' url = 'http://www.tuling123.com/openapi/api'
data = { data = {
'key': os.environ.get('TURING123_API_KEY'), '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) resp = requests.post(url, data=data)
if resp.status_code == 200: if resp.status_code == 200:
json = resp.json() json = resp.json()

View File

@ -13,6 +13,7 @@ import speech_recognition as sr
from filter import as_filter from filter import as_filter
from commands import core from commands import core
from little_shit import get_source
def _recognize_baidu(wav_path, unique_id, api_key, secret_key, language='zh'): def _recognize_baidu(wav_path, unique_id, api_key, secret_key, language='zh'):
@ -74,7 +75,7 @@ def _filter(ctx_msg):
service_full_name = '百度语音识别' service_full_name = '百度语音识别'
text = _recognize_baidu( text = _recognize_baidu(
wav_path, wav_path,
ctx_msg.get('sender_id')[-60:], get_source(ctx_msg),
os.environ.get('BAIDU_SPEECH_API_KEY'), os.environ.get('BAIDU_SPEECH_API_KEY'),
os.environ.get('BAIDU_SPEECH_SECRET_KEY'), os.environ.get('BAIDU_SPEECH_SECRET_KEY'),
language='zh' language='zh'

View File

@ -1,4 +1,5 @@
import os import os
import hashlib
import random import random
from datetime import datetime from datetime import datetime
@ -47,21 +48,24 @@ def get_source(ctx_msg):
Source is used to distinguish the interactive sessions. Source is used to distinguish the interactive sessions.
Note: This value may change after restarting the bot. 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('via') == 'qq':
if ctx_msg.get('type') == 'group_message' and ctx_msg.get('group_uid') and ctx_msg.get('sender_uid'): 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'): 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'): 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': 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'): 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'): elif ctx_msg.get('type') == 'friend_message' and ctx_msg.get('sender_id'):
return 'p' + ctx_msg.get('sender_id') source = 'p' + ctx_msg.get('sender_id')
return str(int(datetime.now().timestamp())) + str(random.randint(100, 999)) 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): def get_target(ctx_msg):
@ -69,7 +73,8 @@ def get_target(ctx_msg):
Target is used to distinguish the records in database. Target is used to distinguish the records in database.
Note: This value will not change after restarting the bot. 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('via') == 'qq':
if ctx_msg.get('type') == 'group_message' and ctx_msg.get('group_uid'): if ctx_msg.get('type') == 'group_message' and ctx_msg.get('group_uid'):