defhelp_menu(msg): if msg == "help": return [True,help_base] return [False]
defadd_data(msg,all_data): one=False if"+"in msg: return add_sxt_data(msg,all_data) if msg.count("+") != 1: return [False] if"/"in msg or"|"in msg: return [True,"不能含有/或|呀~"] if msg.split("+")[1]=="": return [False] msg = msg.split("+") iflen(msg[0])< 2: one=True all_data=read_one() for row in all_data: if msg[0] == row[0]: if msg[1] in row[1]: return [True,"oh,这句话我已经会啦,不用再教我啦~"] row[1].append(msg[1]) save_data(all_data,one) return [True,"添加成功!"] all_data.append([msg[0], [msg[1]]]) save_data(all_data,one) return [True,"添加成功!"]
defsave_data(all_data,one): if one: f=open("./data/talk_data/oneword","w",encoding='UTF-8') else: f = open("./data/talk_data/words","w",encoding='UTF-8') for row in all_data: temp = row[0]+"|"+"".join([i+"/"for i in row[1]]) f.writelines(temp+"\n") f.close()
defdetect_ban(msg,user_id,group_id): if group_id notin group: return [False]
for i in ban_words: s = re.search(i,msg) ifnot s == None: data = { 'user_id':user_id, 'group_id':group_id, 'duration':60 } cq_url = "http://127.0.0.1:5700/set_group_ban" requests.post(cq_url,data=data) return [True,"不要说不该说的话啦~"] return [False]
基于网络 api 接口的字符串处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import urllib from urllib.parse import quote import string import requests defcheck_with_api(msg): url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg=' + msg s = quote(url, safe=string.printable) with urllib.request.urlopen(s) as response: html = response.read() # 将获取到的响应内容进行解码,并将json字符串内容转换为python字典格式 # 通过下标取到机器人回复的内容 with urllib.request.urlopen(s) as response: html = response.read() # 将获取到的响应内容进行解码,并将json字符串内容转换为python字典格式 # 通过下标取到机器人回复的内容 msg=eval(html.decode("utf-8"))["content"].replace('{br}', '\n') msg=msg.replace('菲菲', '若若') msg=msg.replace('未获取到相关信息','找若若有什么事吗?') return msg
from send_message.word_detect import * from random import choice from data.talk_data.base_talk import others_answer from random import randint from data.load_data import read_one from func.api import check_with_api
defmatch(msg,talk_data): if (len(msg)==1): talk_data=read_one() return match_oneword(msg,talk_data) for row in talk_data: if row[0] in msg: x=randint(0,len(row[1])-1) return [True,row[1][x]] return [False,check_with_api(msg)]
defmatch_oneword(msg,talk_data): for row in talk_data: if msg in row[0]: x=randint(0,len(row[1])-1) return [True,row[1][x]] return [False,choice(others_answer["no_answer"])]
# if randint(1,10)<4 or rev['raw_message'] in ban_words or rev['raw_message'] in key_words: return send_message(talk_to_gourp(rev, self.talk_data), rev["group_id"], "group") #return True
功能层
实现一些其它功能,例如群聊定时发送消息。
1 2 3 4 5 6 7
from send_message.send_message import send_message import datetime
deftime_send(h,m,s,msg,qq): now=datetime.datetime.now() if(now.hour==h and now.minute==m and now.second==s): send_message(msg,qq,"group")