def profile_pic(remarkname=None, username=None): if remarkname is None and username is None: return "" if username is None: username = my_glob._to_UN(remarkname) else: remarkname = my_glob._to_NN(username) if username == "None": return "" filename = "|".join(map(str,map(ord, remarkname))) + ".jpg" fileImage = open(filename, "wb") fileImage.write(itchat.get_head_img(userName=username)) fileImage.close() return filename
def first_greeting(to, msg=None): #msg when greeting for the first time to a group, need to print the name of the sender if msg is None: # Friend sig = user.get_signature(username=to) itchat.send('@%s@%s' % ('img', user.profile_pic(username=to)), to) to = my_glob._to_NN(to) else: # Group chat sig = user.get_signature(username=msg['ActualUserName']) itchat.send( '@%s@%s' % ('img', user.profile_pic(username=msg['ActualUserName'])), msg['FromUserName']) to = my_glob._group_member_NN(msg) if to == "None": to = "" return u"你好" + to + bool(sig) * "(" + sig + bool( sig ) * ")" + u",我是小" + your_name + u",請從以下模式中選擇(數字或關鍵字):" + my_glob.greeting_modes + u"\n\n你還可以嘗試發送語音,撤回消息,或者撤回定位和好友名片!" + u"\n\nP.S. 我主人在睡覺!zZ" * my_glob.sleeping + ( my_glob.prompt_message != "") * "\n\n" + my_glob.prompt_message
def SaveMsg(msg): # print(msg) #创建可下载消息内容的存放文件夹,并将暂存在当前目录的文件移动到该文件中 if not os.path.exists(".\\Revocation\\"): os.mkdir(".\\Revocation\\") print("Something just got revoked.") if re.search( r"\<replacemsg\>\<\!\[CDATA\[.*撤回了一条消息\]\]\>\<\/replacemsg\>", msg['Content'] ) != None or re.search( r"\<replacemsg\>\<\!\[CDATA\[.*rappelé un message.\]\]\>\<\/replacemsg\>", msg['Content'].encode('utf-8')) != None: old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) old_msg = my_glob.msg_dict.get(old_msg_id, {}) if old_msg.get('msg_from', "") == my_glob._to_NN(my_glob.me): ## Me !!!! return #print(old_msg_id, old_msg) msg_send = old_msg.get('msg_from', "") \ + u" 在 [" + old_msg.get('msg_time_touser', "") \ + u"], 撤回了一条 ["+old_msg['msg_type']+u"] 消息, 内容如下:" \ + old_msg.get('msg_content', None) if old_msg['msg_type'] == "Sharing": msg_send += u", 链接: " \ + old_msg.get('msg_url', None) elif old_msg['msg_type'] == 'Picture' \ or old_msg['msg_type'] == 'Recording' \ or old_msg['msg_type'] == 'Video' \ or old_msg['msg_type'] == 'Attachment': msg_send += u", 存储在当前目录下Revocation文件夹中" shutil.move(old_msg['msg_content'], u".\\Revocation\\") print(msg_send) itchat.send(msg_send, toUserName='******') #将撤回消息的通知以及细节发送到文件助手 if old_msg.get('send_back', True): # If send back to user itchat.send(msg_send, toUserName=msg['FromUserName']) my_glob.msg_dict.pop(old_msg_id) try: ClearTimeOutMsg() except: pass
def Revocation_helper(msg, send_back=True): #print("Revocation helper called.") mytime = time.localtime() # 这儿获取的是本地时间 #获取用于展示给用户看的时间 2017/03/03 13:23:53 msg_time_touser = mytime.tm_year.__str__() \ + "/" + mytime.tm_mon.__str__() \ + "/" + mytime.tm_mday.__str__() \ + " " + mytime.tm_hour.__str__() \ + ":" + mytime.tm_min.__str__() \ + ":" + mytime.tm_sec.__str__() msg_id = msg['MsgId'] #消息ID msg_time = msg['CreateTime'] #消息时间 ### This doesn't work for groups! Consider reading directly from ### msg_from = my_glob._to_NN(msg['FromUserName']) # For friends if msg_from == "None": msg_from = msg.get('FromUserName', "") + u" 的 " + my_glob._group_member_NN( msg) # For groups if msg_from == u" 的 ": msg_from = u"你" # If some error occurred during retrieving the send-from username msg_type = msg['Type'] #消息类型 msg_content = None #根据消息类型不同,消息内容不同 msg_url = None #分享类消息有url #图片 语音 附件 视频,可下载消息将内容下载暂存到当前目录 if msg['Type'] == 'Text': msg_content = msg['Text'] elif msg['Type'] == 'Picture': msg_content = msg['FileName'] msg['Text'](msg['FileName']) elif msg['Type'] == 'Card': msg_content = msg['RecommendInfo']['NickName'] + u"的名片" elif msg['Type'] == 'Map': x, y, location = re.search( "<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1, 2, 3) if location is None: msg_content = r"緯度->" + x.__str__() + " 経度->" + y.__str__() else: msg_content = r"" + location elif msg['Type'] == 'Sharing': msg_content = msg['Text'] msg_url = msg['Url'] elif msg['Type'] == 'Recording': msg_content = msg['FileName'] msg['Text'](msg['FileName']) elif msg['Type'] == 'Attachment': msg_content = r"" + msg['FileName'] msg['Text'](msg['FileName']) elif msg['Type'] == 'Video': msg_content = msg['FileName'] msg['Text'](msg['FileName']) elif msg['Type'] == 'Friends': msg_content = msg['Text'] #更新字典 # {msg_id:(msg_from,msg_time,msg_time_touser,msg_type,msg_content,msg_url)} my_glob.msg_dict.update({ msg_id: { "msg_from": msg_from, "msg_time": msg_time, "msg_time_touser": msg_time_touser, "msg_type": msg_type, "msg_content": msg_content, "msg_url": msg_url, "send_back": send_back } }) #清理字典 try: ClearTimeOutMsg() except: pass
def process(msg, group=False): #if msg['FromUserName'] == my_glob.me: # return #msg['RemarkName'] = my_glob.UserName_to_name(msg['FromUserName']) #if msg['RemarkName'] == None: # print("No remark name for this person. Returned.") # return if time.time() - my_glob.last_save_time > 300: my_glob.params.write_all() my_glob.last_save_time = time.time() msg['Text'] = msg['Text'].rstrip(".").rstrip(u"。") ## if msg['FromUserName'] == my_glob.me and msg['Text'].startswith("$"): command(msg['Text'][1:]) return if my_glob.params.get("time", msg['FromUserName']): if my_glob.params.get("mode", msg['FromUserName']) == "shutup" and ( time.time() - my_glob.params.get("time", msg['FromUserName']) > 18000.0): ## If shutup, reset after 5 hrs instead of 20 mins fundamental.mysend( u"上次閉嘴時間為%d分鐘前,超過五小時,已重置。\n" % int( (time.time() - my_glob.params.get("time", msg['FromUserName'])) / 60), msg['FromUserName']) my_glob.params.set(task="mode", UserName=msg['FromUserName'], value=None) elif my_glob.params.get("mode", msg['FromUserName']) != "shutup" and ( time.time() - my_glob.params.get("time", msg['FromUserName']) > 1200.0): fundamental.mysend( u"您上次玩耍時間為%d秒前,超過二十分鐘,已重置。\n" % (time.time() - my_glob.params.get("time", msg['FromUserName'])), msg['FromUserName']) my_glob.params.set( task="mode", UserName=msg['FromUserName'], value=None ) # It's possible that someone stopped choosing the mode and then time elapsed for more than 10 min, so initialize mode my_glob.params.set(task="time", UserName=msg['FromUserName']) if (len(msg['Text']) <= 3 and msg['Text'].startswith(u"停")) or msg['Text'].upper( ).startswith("F**K") or msg['Text'].upper().startswith( "STOP") or u"[On Fire]" in msg['Text'] or u"傻逼" in msg['Text']: fundamental.mysend("[Silence][Distressed]", msg['FromUserName']) return if (my_glob.params.get(task="mode", UserName=msg['FromUserName']) is None) or msg['Text'] in my_glob.change_chinese: if group: fundamental.mysend("#GREETING1#", msg['FromUserName'], msg) # If group else: fundamental.mysend("#GREETING1#", msg['FromUserName']) my_glob.params.set(task="mode", UserName=msg['FromUserName'], value="CHANGE") my_glob.params.set( task="params", UserName=msg['FromUserName'], value=None, mode="stock" ) # If change mode from stock, need to reset stock ticker # The set function takes care of the case with no stock return if my_glob.params.get(task="mode", UserName=msg['FromUserName']) == "shutup": if (u"機器人" in msg['Text'] and (u"出來" in msg['Text'] or u"回來" in msg['Text'] or u"出現" in msg['Text'])) or ( u"机器人" in msg['Text'] and (u"出来" in msg['Text'] or u"回来" in msg['Text'] or u"出现" in msg['Text'])) or (u"機器人呢" in msg['Text']) or ( u"机器人呢" in msg['Text']) or (msg['Text'] in [ u"召唤", u"召喚", u"召回", u"召唤!", u"召喚!", u"召回!", u"召唤!", u"召喚!", u"召回!" ]): fundamental.mysend(u"我在這呢!\n要我回來,請說換!", msg['FromUserName']) return if msg['Text'].startswith(u"闭嘴") or msg['Text'].startswith( u"閉嘴") or msg['Text'].startswith(u"收皮"): my_glob.params.set("mode", msg['FromUserName'], value="shutup") fundamental.mysend(my_glob.modes_greeting["shutup"], msg['FromUserName']) return if msg['Text'] == u"我": fundamental.mysend( u"你是" + (my_glob._to_NN(msg['FromUserName']) or u"誰?"), msg['FromUserName']) return if msg['Text'] == u"你": fundamental.mysend(u"我是小" + your_name + "!", msg['FromUserName']) return #if my_glob.params.get(task="mode", UserName=msg['FromUserName']) == "CHANGE": for mode in [ "chat", "google_translate", "chiuman", "weather", "stock", "mw", "ytenx", "wiki" ]: ## In any mode, if user input is a mode name, switch to that mode if msg['Text'] == str( my_glob.modes_indices[mode] ) or msg['Text'] in my_glob.modes_alt_names[mode]: my_glob.params.set("mode", msg['FromUserName'], mode) if mode == "stock": # If stock, send indices first and then greeting fundamental.mysend(stock.get_indices(), msg['FromUserName']) fundamental.mysend(my_glob.modes_greeting[mode] + u"\n(此後更換模式請輸入「換」,或直接輸入模式名)", msg['FromUserName']) # Send greeting if mode == "chiuman": # If chiuman, send greeting first and then start stories fundamental.mysend(chiuman.chiuman(msg), msg['FromUserName']) return if msg['Text'] == str( my_glob.modes_indices["shutup"] ): ## In any mode, if user input is a mode name, switch to that mode my_glob.params.set("mode", msg['FromUserName'], value="shutup") fundamental.mysend( my_glob.modes_greeting["shutup"] + u"\n(此後更換模式請輸入「換」,或直接輸入模式名)", msg['FromUserName']) return elif msg['Text'] == str( my_glob.modes_indices["reverse"] ) or msg['Text'] in my_glob.modes_alt_names[ "reverse"]: ## In any mode, if user input is a mode name, switch to that mode my_glob.params.set("mode", msg['FromUserName'], value="reverse") try: to = my_glob._to_NN(msg['FromUserName']) ## Individual friend chat except: try: to = msg['ActualNickName'] ## Group chat except: to = u"客官" fundamental.mysend((u"好的," + to)[::-1] + u"\n(此後更換模式請輸入「換」,或直接輸入模式名)", msg['FromUserName']) return elif msg['Text'] == str( my_glob.modes_indices["twitter"] ) or msg['Text'] in my_glob.modes_alt_names[ "twitter"]: ## In any mode, if user input is a mode name, switch to that mode my_glob.params.set("mode", msg['FromUserName'], value="twitter") try: userlangs = [ my_glob.lang_names[la] for la in my_glob.params.get( "params", msg['FromUserName'], "lang") ] except: userlangs = [u"中文", u"英語"] fundamental.mysend( my_glob.modes_greeting["twitter"] % (u"、".join(userlangs[:-1]) + u"和" + userlangs[-1]) + u"\n(此後更換模式請輸入「換」,或直接輸入模式名)", msg['FromUserName']) return elif my_glob.params.get( task="mode", UserName=msg['FromUserName'] ) == "CHANGE": ## If user really is in the change mode but entered wrong input if msg['Text'] == "0": fundamental.mysend( u"[Smile]你自己就是個0吧[Smile]說了讓你輸1-%s![Smile]" % (len(my_glob.modes) - 1), msg['FromUserName']) return elif msg['Text'] == str(len(my_glob.modes)): fundamental.mysend( u"[Smile]你以為我不知道你輸了" + str(len(my_glob.modes)) + u"?[Smile]", msg['FromUserName']) return else: # Really wrong input fundamental.mysend("#GREETING2#", msg['FromUserName']) return #my_glob.params.set("params", msg['FromUserName'], None) # Not sure why this was here return # Can return here if user is meant to change mode # Otherwise, continue with the current mode if my_glob.params.get("mode", msg['FromUserName']) == "chat": send_text = auto.auto(msg) elif my_glob.params.get("mode", msg['FromUserName']) == "google_translate": send_text = translate.google_translate(msg) elif my_glob.params.get("mode", msg['FromUserName']) == "chiuman": send_text = chiuman.chiuman(msg) elif my_glob.params.get("mode", msg['FromUserName']) == "reverse": send_text = reverse(msg) elif my_glob.params.get("mode", msg['FromUserName']) == "weather": send_text = my_weather.weather(msg) elif my_glob.params.get("mode", msg['FromUserName']) == "stock": send_text = stock.get_stock(msg) elif my_glob.params.get("mode", msg['FromUserName']) == "twitter": twitter.good_lang = (my_glob.params.get("params", msg['FromUserName'], "lang") or ["zh", "en"]) send_text = twitter.tweet() elif my_glob.params.get("mode", msg['FromUserName']) == "mw": send_text = mw.lookup(msg['Text']) elif my_glob.params.get("mode", msg['FromUserName']) == "ytenx": res = ytenx.ytenx(msg['Text']) for send_text in res: if send_text != "": fundamental.mysend(send_text, msg['FromUserName']) elif my_glob.params.get( "mode", msg['FromUserName'] ) == "wiki": ## Immediately reset after wiki is done so that user will not search again by mistake try: send_text = safe_exec("wiki.wiki(msg)", msg) except: send_text = u"維基百科抓取失敗。" signal.alarm(0) fundamental.mysend(send_text + u"\n已為您重置回主菜單。", msg['FromUserName']) my_glob.params.set(task="mode", UserName=msg['FromUserName'], value="CHANGE") if group: fundamental.mysend("#GREETING1#", msg['FromUserName'], msg) # If group else: fundamental.mysend("#GREETING1#", msg['FromUserName']) return fundamental.mysend(send_text, msg['FromUserName'])