def get_answer(msg: mrc.Msg): global __user_qa question = __user_qa.get(msg.FromUserName) answer = spider.all_qas.get(question, "你先说 “出题”") reply_msg = mrp.TextMsg(msg.FromUserName, msg.ToUserName, answer) return reply_msg.send()
def test_process_text_msg(): to_user_name = 'to_user_name' from_user_name = 'from_user_name' content = 'content' text_msg = mrp.TextMsg(to_user_name, from_user_name, content).send() assert text_msg != "success"
def unknow_purpose(msg: mrc.Msg): mapping = { 'text': '老板,我不明白你写的什么', 'image': '老板,我这智商也就告别图片了', 'voice': '老板,我不明白你说的什么', 'video': '老板,我这智商也就告别视频了', 'shortvideo': '老板,我这智商也就告别短视频了', 'location': '老板,你给我发地址也没有用啊', 'link': '老板,这个链接是让我学习的吗', } return mrp.TextMsg(msg.FromUserName, msg.ToUserName, mapping[msg.MsgType]).send()
def random_question(msg: mrc.Msg): global __user_qa all_questions = list(spider.all_qas.keys()) question_count = len(all_questions) rd_num = random.randint(0, question_count-1) question = all_questions[rd_num] __user_qa[msg.FromUserName] = question reply_msg = mrp.TextMsg(msg.FromUserName, msg.ToUserName, '[随机问题] ' + question) return reply_msg.send()
def specify_question(msg: mrc.Msg): global __user_qa all_question = spider.all_qas.keys() text = __get_text_from_msg(msg) extract_result = process.extractOne(text[4:], all_question) # 匹配率大于 50% 的返回指定问题 if extract_result[1] > 50: __user_qa[msg.FromUserName] = extract_result[0] reply_msg = mrp.TextMsg(msg.FromUserName, msg.ToUserName, extract_result[0]) return reply_msg.send() # 否则返回随机问题 else: return random_question(msg)
def POST(self): try: data = web.data() print(f"handle/POST 接受参数 {data}") # 消息处理 rec_msg = mrc.parse_xml(data) if not rec_msg: return 'success' if isinstance(rec_msg, mrc.ErrMsg): return mrp.TextMsg(to_user_name=rec_msg.FromUserName, from_user_name=rec_msg.ToUserName, content=rec_msg.err).send() # 根据用户信息获取用户身份 identity = mt_auth.auth_verify(rec_msg) # 根据消息获得用户行为 purpose = mt_choice.get_purpose_by_msg(identity, rec_msg) # 根据用户身份和行为执行相应动作 return purpose(rec_msg) except Exception as err: print(f"handle/POST {type(err)} 异常, msg => {err}") return err
def permission_denied_purpose(msg: mrc.Msg): content = '权限不足,请联系管理员开通' return mrp.TextMsg(msg.FromUserName, msg.ToUserName, content).send()
def flush_db(msg: mrc.Msg): spider.flush_db() content = "数据库刷新完成" reply_msg = mrp.TextMsg(msg.FromUserName, msg.ToUserName, content) return reply_msg.send()