def getAdmin(self, name=""): level = self.admins.get(self.userIndex.get(name, ""), 0) # self.adminRes = 0 # self.adminDeta = 2 # self.adminDetaDefault = 2 # <2 <4 <8 <16 <32 #i:1 2 3 4 5 # +2 +4 +8 +16 +32 i = 1 weight = 1 while(True): weight = weight * self.adminDetaDefault # *= 2 if(level < weight): break i = i + 1 # level=0-2 -> i=1 weight=2 res = "" ranres = "" for j in range(i): ran = tool.getRandom(1000, 9999) # 2843 = 287 = 35 = 8 ans = ran while(ans >= 10): ans = ans % 10 + ans / 10 res = res + str(ans) ranres = ranres + str(ran) + " " self.adminRes = res self.adminDeta = weight # self.showAdmin() self.send("#Admin认证 lv." + str(i) + "\nF(" + str(ranres) + ") = ? " )
def turnMusic(self, playType): music = {} if (playType == -1 and len(self.palyHistoryMusic) > 1): #上一曲 music = self.palyHistoryMusic.pop() #弹出顶 取末 music = self.palyHistoryMusic[-1] else: cc = 0 while (music.get("url", "") == ""): size = self.db.getCount("select * from music ") if (size <= 0): break num = 5 page = int(1.0 * size / num) page = tool.getRandom(0, page) (size, listRes) = self.db.executeQueryPage("select * from music", page, num) getSize = len(listRes) count = tool.getRandom(0, getSize) music = listRes[count] tool.line() self.out("随机找到歌曲页 " + "size:" + str(size) + " page:" + str(page) + " num:" + str(num) + " listResSize:" + str(getSize)) for item in listRes: self.out("url:" + item.get("url") + " name:" + item.get("name") + " fromName:" + item.get("fromName") + " time:" + str(tool.calcTime(item.get("duration", 0)))) self.out("选中了" + str(count)) tool.line() url = music.get("url", "") if (self.http.existAudio(url)): break else: self.out("访问音乐文件失败" + url) self.removeMusic(url) music = {} if (cc > 20): break cc = cc + 1 return music
def sayHello(self): while(True): if(self.roomId != ""): self.out("开启定时发言,最大发言间隔" + str(self.maxDetaTime / 1000) + "s") sleepTime = 3 dt = 0 theI = 0 self.lastEchoTimeQuene = tool.getNowTime() while(self.roomId != ""): try: # message = "Now Time is "+ time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) detaTime = tool.getNowTime() - self.lastEchoTime # ms if(detaTime > self.maxDetaTime): # message = "存活确认." + str(theI) + "." + ttt = time.strftime("%H時%M分%S秒") message = "現在時間是" + ttt + ",每隔5分鐘免費派送一座冰山:△,一間冰窟酒吧:♜,一棟冰窟豪宅:▌,一缸冰:,一缸涼水:,附加贈送一個月的南極大陸度假旅遊服務(可免費觀看地下冰花:✹✺✻✼✽✾✿❀❁❂❃❄❅❆❇❈❉❊❋。)。" self.doSendId(message) self.out(str(theI) + "\t" + message) theI = theI + 1 self.lastEchoTime = tool.getNowTime() detaTime = tool.getNowTime() - self.lastOtherSay # ms if(detaTime > self.maxDetaOtherSay): #不不停留True self.goARoom() #10分钟没处理过消息 互动 则换房间 if(detaTime > self.maxDetaOtherSay * 2): #若一小时没信息 则 是否掉线? self.shutdown() #等待系统重启 if(dt % 600 == 0): self.getRooms() #定时5分钟获取房间最新信息 if(dt % 120 == 0): roomsAdmin = self.getUserRoom(self.flowName); if(len(roomsAdmin) > 0): self.out("跟随 触发") self.goRoom(roomsAdmin[tool.getRandom(0, len(roomsAdmin))].get("id", "")) time.sleep(sleepTime) dt = dt + sleepTime dt = dt % 3600 except Exception as e: self.out(traceback.format_exc()) # self.out("当前房间roomId:" + self.roomId + " 未加入房间 暂时停止sayHello ") time.sleep(10) if(self.roomId == ""): #无房间则自动加入 self.goARoom()
def makeHello(self, obj): res = "" try: # tool.line() # self.out("抓取到消息obj") # self.out(obj) newTime = obj.get("update", self.lastMsgTime) newTime = int(newTime) # print(newTime) self.lastMsgTime = newTime talks = obj.get('talks', "") users = obj.get('users', "") if(users != ""): room = self.roomIndex.get(self.roomId, "") if(room != ""): self.roomIndex[self.roomId]['users'] = users else: self.roomIndex[self.roomId] = obj if(talks != ""): onceDocount = 0 for item in talks: # self.out(item) msgTime = item.get("time", tool.getNowTime()) msgId = item.get('id', " ") secret = item.get('secret', False) toId = item.get('to', {}).get('id', '') toName = item.get('to', {}).get('name', '') msgType = item.get('type', 'message') msgData = "" msgFromName = item.get('from', {}).get('name', "") fromId = item.get('from', {}).get('id', "") fromCode = item.get('from', {}).get('tripcode', '') if(msgFromName == ""): msgFromName = item.get('user', {}).get('name', "") fromId = item.get('user', {}).get('id', "") fromCode = item.get('user', {}).get('tripcode', '') turnFlag = 0 if(msgFromName != "" and fromId != "" and self.roomMsg.get(msgId, "") == ""): self.userIndex.get(msgFromName) # 处理同名 tripcode认证继承 异id问题 名字对应id不一样了 该房间里的cc不是原来记录的了 则删除原来的admin 顶替 n:id--1:名字 if(self.userIndex.get(msgFromName, fromId) != fromId): #非同id oldId = self.userIndex.pop(msgFromName) if(self.userIndexRe.get(oldId, "") != ""): self.userIndexRe.pop(oldId) # 判定和上次记录 的 zk#aa zk# 是否匹配 绑定用户名和code lastCode = self.tripcodeIndex.get(msgFromName, "") if(lastCode == fromCode): #但是 tc code 正常 则转移 if(self.admins.get(oldId,"") != ""): #若有admin则继承 oldValue = self.admins.pop(oldId) self.addAdmin(fromId, int(oldValue)) turnFlag = 1 #欢迎回来 else: # 否则 没有tc 同名 覆盖 都有tc 但不同 则也覆盖 if(self.admins.get(oldId,"") != ""): #若有admin则继承 oldValue = self.admins.pop(oldId) self.tripcodeIndex[msgFromName] = fromCode self.send("[" + msgFromName + "] tc变更[" + str(lastCode) + " - " + str(fromCode) ) self.userIndex[msgFromName] = fromId self.userIndexRe[fromId] = msgFromName if(msgType == 'me'): msgData = item.get('content', "") elif(msgType == 'message'): msgData = item.get('message', "") elif(msgType == 'join' and self.ifWelcom): # msgFromName = item.get('user', {}).get('name', "") if(turnFlag == 0): msgData = '欢迎' + msgFromName + self.tail else: msgData = '欢迎回来 ' + msgFromName + self.tail elif(msgType == 'leave'): msgData = '' + msgFromName + '' + self.tail msgData = '' elif(msgType == 'music'): music = item.get('music', {}) name = music.get('name', '') url = music.get('url', '') # msgData = '悄悄的的把[' + name + ']给记在小本子上 ' + self.tail ######################################################## 不处理 if( self.roomMsg.get(msgId, "") != ""): #已经处理过 或者是自己发送的 或者取出发送者失败 # self.out("旧消息 " + msgId + " type:" + msgType + " data:" + msgData) continue self.roomMsg[msgId] = item #标记未已经处理 历史消息 if( msgFromName == self.name or msgFromName == ""): continue if(msgType == "me" or msgType == "message"): #只记录聊天消息 self.robot.addMsg(msgId, msgFromName, msgData, msgTime) ############################################################# #当前消息信息记录 self.fromNameNow = msgFromName self.fromIdNow = fromId self.fromNameNow = msgFromName self.toIdNow = toId self.toNameNow = toName if(msgType == 'music'): music = { "name":name, "url":url, "fromName":msgFromName } res = self.robot.addMusic(music) #添加用户分享记录 if(res): self.robot.addHistory(music) self.musicNow = music self.lastMusicTime = tool.getNowTime() # self.roomMsg[msgId] = item #标记未已经处理 历史消息 if(self.status>self.statusMax): self.status = self.statusMax elif(self.status < self.statusMin): self.status = self.statusMin detaTime = tool.getNowTime() - self.lastEchoTimeQuene # ms 60s olRan = tool.getRandom(0,self.maxDetaTime) / 1000 #0-180 过于久没有发过消息了 权重高则可能自回复 weight = (self.maxDetaTime - detaTime) / 1000 #多久没说话了 最大多长时间必须说话 ran = int(1.0 * olRan * (1+ 1.0 * (self.status-90) / 100) ) # self.out("Msg." + msgId[0:4] + "." + tool.fill(str(weight) + "" , ' ', 5) + " " + tool.fill(str(olRan) + "->" + str(ran),' ', 5) + "." + tool.fill(msgFromName,' ',8) + "."+tool.fill(msgType,' ',4) + "." + msgData + " ." + str(fromId)) self.out("Msg." + msgId[0:4] + " " + msgFromName[0:10] + "->" + toName[0:10]+ " "+msgType[0:4] + " " + msgData ) msgData = msgData.strip() flag = 0 #不回复 if(msgType == 'message' or msgType == 'me' ): #普通聊天消息 if( re.search('@' + self.name + " ", msgData) != None): #有@自己 且权重不太低 msgData = re.sub('@' + self.name + "", "", msgData) #摘除@自己 flag = 1 msgData = msgData.strip() # else: # self.out("@me 随机数=" + str(ran) + " 小于 说话欲望=" + str(self.status) + " ") # flag = 2 # msg = "生气程度:" + str(100-self.status) + "%,不想搭理"+self.tail elif(ran > weight and re.search('@', msgData) == None): # 没有@ 且 权重高 主动搭话概率 flag = 1 elif(toId != ""): #私聊 flag = 1 else: flag = 10 #admin权限认证 if(self.adminRes != "" and str(msgData) == str(self.adminRes)): self.out("触发权限admin认证." + str(self.adminRes) + "=" + str(msgData) + "." + msgFromName + "." + fromId) self.send("认证成功[" + str(msgFromName) + "]") self.addAdmin(fromId) flag = 0 msgData = "" else: #事件 flag = 2 res = "" self.lastOtherSay = tool.getNowTime() #重置处理时间 黑名单消息不计入消息 if(self.filterFlag(msgData, msgFromName)): #最高级 权限是否黑名单过滤 if(flag == 1 or flag == 10): if(flag == 1 and self.robot.getUser(msgFromName).get("flag", "0") != "0"): self.out("不想搭理" + msgFromName) else: if(flag == 1 and self.filterCmd(msgData, msgFromName)): #若过滤器未处理 则继续交由下面处理 pass elif(flag == 10): #让普通消息也接入 cmd 不过没有后续处理 self.filterCmd(msgData, msgFromName) res = "" elif(flag == 2): res = msgData if(res != "" and flag != 0 and onceDocount < 6): # 最多一次抓取发送3个 res = '' + res onceDocount = onceDocount + 1 # self.send(res) #当前消息信息清空 self.fromNameNow = "" self.fromIdNow = "" self.fromNameNow = "" self.toIdNow = "" self.toNameNow = "" except Exception as e: self.out("Exception:" + str(e)) # tool.line() return res
def makeHello(self, obj): res = "" try: # tool.line() # self.out("抓取到消息obj") # self.out(obj) newTime = obj.get("update", self.lastMsgTime) newTime = int(newTime) # print(newTime) self.lastMsgTime = newTime talks = obj.get('talks', "") users = obj.get('users', "") if (users != ""): room = self.roomIndex.get(self.roomId, "") if (room != ""): self.roomIndex[self.roomId]['users'] = users else: self.roomIndex[self.roomId] = obj if (talks != ""): onceDocount = 0 for item in talks: # self.out(item) msgTime = item.get("time", tool.getNowTime()) msgId = item.get('id', " ") msgType = item.get('type', 'message') msgData = "" msgFromName = item.get('from', {}).get('name', "") if (msgFromName == ""): msgFromName = item.get('user', {}).get('name', "") if (msgType == 'me'): msgData = item.get('content', "") elif (msgType == 'message'): msgData = item.get('message', "") elif (msgType == 'join'): # msgFromName = item.get('user', {}).get('name', "") msgData = '欢迎' + msgFromName + self.tail elif (msgType == 'leave'): msgData = ' ' + msgFromName + ' 默默的离开了 ' + self.tail elif (msgType == 'music'): music = item.get('music', {}) name = music.get('name', '') url = music.get('url', '') # msgData = '悄悄的的把[' + name + ']给记在小本子上 ' + self.tail ######################################################## 不处理 if (self.roomMsg.get(msgId, "") != ""): #已经处理过 或者是自己发送的 或者取出发送者失败 # self.out("旧消息 " + msgId + " type:" + msgType + " data:" + msgData) break if (msgType == "me" or msgType == "message"): #只记录聊天消息 self.robot.addMsg(msgId, msgFromName, msgData, msgTime) if (msgFromName == self.name or msgFromName == ""): break ############################################################# if (msgType == 'music'): music = { "name": name, "url": url, "fromName": msgFromName } res = self.robot.addMusic(music) #添加用户分享记录 if (res == 1): #更新则不提示 msgData = "" self.musicNow = music self.lastMusicTime = tool.getNowTime() self.roomMsg[msgId] = item #标记未已经处理 历史消息 if (self.status > self.statusMax): self.status = self.statusMax elif (self.status < self.statusMin): self.status = self.statusMin detaTime = tool.getNowTime() - self.lastEchoTime # ms 60s olRan = tool.getRandom( 0, self.maxDetaTime) / 1000 #0-180 过于久没有发过消息了 权重高则可能自回复 weight = (self.maxDetaTime - detaTime) / 1000 #多久没说话了 最大多长时间必须说话 ran = int(1.0 * olRan * (1 + 1.0 * (self.status - 60) / 100)) self.out("新消息 " + msgId + " 发言权" + tool.fill(str(weight) + "", ' ', 6) + " 随机数" + tool.fill(str(olRan) + "->" + str(ran), ' ', 6) + " from:" + tool.fill(msgFromName, ' ', 12) + " type:" + tool.fill(msgType, ' ', 6) + " data:" + msgData) flag = 0 #不回复 if (msgType == 'message' or msgType == 'me'): #普通聊天消息 if (re.search('@' + self.name + " ", msgData) != None): #有@自己 且权重不太低 msgData = re.sub('@' + self.name + " ", "", msgData) #摘除@自己 ran = tool.getRandom(0, 100) if (ran < self.status): flag = 1 else: self.out("@me 随机数=" + str(ran) + " 小于 说话欲望=" + str(self.status) + " ") flag = 2 msg = "生气程度:" + str( 100 - self.status) + "%,不想搭理" + self.tail elif (ran > weight and re.search('@', msgData) == None): # 没有@ 且 权重高 主动搭话概率 flag = 1 else: #事件 flag = 2 res = "" if (self.filterFlag(msgData, msgFromName)): #最高级 权限是否黑名单过滤 if (flag == 1): if (self.robot.getUser(msgFromName).get( "flag", "0") != "0"): self.out("不想搭理" + msgFromName) else: if (self.filterCmd( msgData, msgFromName)): #若过滤器未处理 则继续交由下面处理 robotRes = self.robot.do( msgData, self.name) code = str(robotRes.get("code", "")) if (code[0:1] != '4'): res = '@' + str( msgFromName) + " " + robotRes.get( "text", "") else: self.out("robot接口调用失败 code=" + code) elif (flag == 2): res = msg if (res != "" and flag != 0 and onceDocount < 6): # 最多一次抓取发送3个 res = '/me ' + res onceDocount = onceDocount + 1 self.send(res) except Exception as e: self.out("Exception:" + str(e)) # tool.line() return res
def message(self, *args): # 普通消息 try: # print("收到message ") # print(args) # tool.line() # print("转换") args = self.turnArray(args) # print(args[0]) # print(args[1]) if(len(args) <= 1): return data = args[1] #{} mtype = args[0] #message null fun if(mtype == 'message'): data = tool.toJson(data) fro = data.get("from", {}) to = data.get("to", {}) contact = data.get("contact", {}) msg = data.get("body") fullId = contact.get("fullId") sessionName = contact.get("nickName", "") uid = self.data.get("uuid") tTag = data.get("timeMillis", tool.getNowTime()) tool.line() self.out("Msg:" + fro.get("nickName","from") + ">>" + msg + ">>" + to.get("nickName","to") + " time:" + data.get("time")) # self.out(msg); # 自发消息不需要处理 if(fro.get("nickName","from").find(self.loginUser.get("ORG_VARS", {}).get("@USER_NAME@", "")) >= 0): return ttt = self.detaTime reg = re.match(r'^\d+$', str(msg)) if(reg is not None): ttt = int(msg) * 1000 self.send("updateConversationStatus", { 'contactFullId': fullId, 'clientId': uid, 'timeTag': tTag }, ttt) self.send("updateMsgStatus", { "messages":data.get("id","") }, ttt) # 自发言 且 只有自己auto回复 if(self.id != "18408249138"): return # 过滤 ff = sessionName.find("陈鹏辉") hh = fro.get("nickName","from").find("许欢") zz = fro.get("nickName","from").find("赵振国") cdf = fro.get("nickName","from").find("迪") d1 = sessionName.find("迪") d2 = sessionName.find("祝") d3 = sessionName.find("纯") if(ff < 0 or d1 >= 0 or d2 >= 0 or d3 >= 0): self.out("未命中自己title 命中特殊 不回复") return point = 76 if(hh >= 0 or zz >= 0): point = 33 self.sendTrue("updateMsgStatus", { "messages":data.get("id","") }) if(tool.getRandom(0,100) < point): self.out("概率不自动回复" + str(point)) return # data["body"] = str(data["body"]) + "." # self.send("message", data) obj = {} # if(contact.get("type") == "GROUP"): # unicode(self.robot.do(msg, fro.get("nickName"))) msg = self.robot.do(msg, fro.get("nickName")) msg = msg.get("text", ''); if(msg == ''): return obj["body"] = msg #"666" + str(tool.getNowTime()) obj["bodyType"] = "text" obj["clientId"] = str(uuid.uuid1()) obj["retry"] = 1 obj["from"] = {} obj["from"]["fullId"] = "u__" + self.data.get("userName") obj["from"]["id"] = self.data.get("userName") obj["from"]["nickName"] = "fromnickname" obj["to"] = {} if(contact.get("type") == "GROUP"): obj["to"]["fullId"] = contact.get("fullId") else: obj["to"]["fullId"] = fro.get("fullId") obj["to"]["nickName"] = "tonickname" obj["from"]["nickName"] = self.loginUser["ORG_VARS"]["@USER_NAME@"] # "from-nickName" obj["to"]["nickName"] = "to-nickName" self.sendTrue("message", obj) elif(str(mtype) == 'fun'): pass elif(str(mtype) == 'event'): pass else: self.out("其他:" + str(mtype)) print(data) # print(args) # tool.line() except Exception as e: self.out(traceback.format_exc()) return