def text_reply(msg): # 监听指定微信公众号推送的文章信息 print msg if itchat.search_mps(name='PythonCoder')[0]["NickName"] == "PythonCoder": # 爬取的是itchat.search_mps(name='PythonCoder')[0]["NickName"]的公众号文章 if msg["MsgType"] == 49: print "监听到指定微信公众号分享的文章链接:" # 拿到链接以后就可以获取到文章信息 print msg["Url"] else: print "微信公众号分享的不是文章" elif itchat.search_mps(name='非著名程序员')[0]["NickName"] == "非著名程序员": if msg["MsgType"] == 49: print "监听到指定微信公众号分享的文章链接:" # 拿到链接以后就可以获取到文章信息 print msg["Url"] else: print "微信公众号分享的不是文章" elif itchat.search_mps(name='程序员大咖')[0]["NickName"] == "程序员大咖": if msg["MsgType"] == 49: print "监听到指定微信公众号分享的文章链接:" # 拿到链接以后就可以获取到文章信息 print msg["Url"] else: print "微信公众号分享的不是文章" elif itchat.search_mps(name='iOS程序员大咖')[0]["NickName"] == "iOS程序员大咖": if msg["MsgType"] == 49: print "监听到指定微信公众号分享的文章链接:" # 拿到链接以后就可以获取到文章信息 print msg["Url"] else: print "微信公众号分享的不是文章" else: pass
def text_reply(msg): print msg print itchat.search_mps(name='PythonCoder')[0]["NickName"] if msg["MsgType"] == 49: print "监听到制定微信公众号分享的文章链接:" print msg["Url"] else: print "微信公众号分享的不是文章"
def get_public_number(): # 获取公众号信息 all=itchat.get_mps() print(all) # 搜索公众号 some=itchat.search_mps(userName='******') print(some) other=itchat.search_mps(name='LitterCoder')
def wx_job(): for mpName in SIGN_IN_MP_DICT.keys(): mpList = itchat.search_mps(name=mpName) if len(mpList) != 1: print(u'没有检测到公众号“%s”,请检查名称') break for k, v in SIGN_IN_MP_DICT.items(): itchat.send(msg=v, toUserName=itchat.search_mps(name=k)[0]['UserName']) print("签到消息发送成功")
def xiaobing(msg): # 发送图灵机器人回复内容 if isTuling: time.sleep(random.random()*2) itchat.send(tuling_reply(msg),toUserName=itchat.search_mps(name='小冰')[0]['UserName']) # 发送之前的内容 if isXiaobingChat: time.sleep(random.random()*3) itchat.send(msg['Content'],toUserName=itchat.search_mps(name='小冰')[0]['UserName']) del msg # 对象的别名被显式的销毁,引用计数值为0,等待垃圾回收。该释放的变量及时释放,如果不及时释放,长期积累占用内存
def groupchat_reply(msg): global msg_from msg_from = '' if msg['FromUserName'] == itchat.search_chatrooms(name='不应该存在')[0]['UserName']: if msg['isAt']: mssg = msg['Content'] itchat.send(mssg[mssg.find(' ') + 1:], toUserName = itchat.search_mps(name='小冰')[0]['UserName']) else: itchat.send(msg['Content'], toUserName = itchat.search_mps(name='小冰')[0]['UserName']) #itchat.send(u'@%s\u2005' % (msg['ActualNickName']) + url_req(str[str.find(' ') + 1:]) + '😀', msg['FromUserName']) msg_from = msg_from + 'chatroom' print('Daky:', msg['Content'])
def getMps(): mps = itchat.get_mps() mps = itchat.search_mps(name='tkBank') #print(mps) userName = mps[0]['UserName'] #itchat.send(msg=question,toUserName = userName) return userName
def xbAnswer(msg): global userId userId = msg['FromUserName'] xb = itchat.search_mps(name='小冰')[0] isGroup(msg) if msg['Type'] == 'Picture': msg['Text'](msg['FileName']) itchat.send_image(msg['FileName'], toUserName=xb['UserName']) os.remove(msg['FileName']) elif msg['Type'] == 'Video': msg['Text'](msg['FileName']) itchat.send_video(msg['FileName'], toUserName=xb['UserName']) os.remove(msg['FileName']) elif msg['Type'] == 'Recording': msg['Text'](msg['FileName']) #保存为mp3 voice = SpeechToText.voice_recon_run(msg['FileName']) if (voice == None): itchat.send_msg("我现在听力不好", toUserName=msg['FromUserName']) else: itchat.send_msg(voice, toUserName=xb['UserName']) else: if ('http' in msg['Text']): dealLink(msg) itchat.send_msg(msg['Text'], toUserName=xb['UserName'])
def run(self): itchat.auto_login(enableCmdQR=2, hotReload=True) search_res = itchat.search_mps(name='小冰') print(json.dumps(search_res, indent=4)) self.xiaoice_id = search_res[0]['UserName'] self.register() itchat.run(debug=True)
def search_OfficialAccount(self, name_str): official_account = itchat.search_mps(name=name_str) #for i in range(0, len(official_account)): # print('UserName: %s' % official_account[i]['UserName']) # print('NickName: %s' % official_account[i]['NickName']) # print('Signature: %s' % official_account[i]['Signature']) return official_account
def send_mps(): ## 获取名字中含有特定字符的公众号,也就是按公众号名称查找,返回值为一个字典的列表 mps = itchat.search_mps(name='CSDN') print mps #发送方法和上面一样 userName = mps[0]['UserName'] itchat.send("hello",toUserName = userName)
def text_reply(msg): global msg_fromevernot msg_from = '' if itchat.search_friends(userName=msg['FromUserName'])['RemarkName'] == friend or \ itchat.search_friends(userName=msg['FromUserName'])['NickName'] == friend: itchat.send(msg['Content'], toUserName = itchat.search_mps(name='小冰')[0]['UserName']) msg_from = msg_from + 'friend'
def text_reply(msg): result = itchat.search_friends() # 获取自己的信息 if msg.FromUserName != result.UserName: # 自己的消息不发送给小冰 r.set('fromuser', msg.fromUserName, 10) # 保存最近一个发送人的id author = itchat.search_mps(name='小冰')[0] # 获取小冰的信息-需要先关注微软小冰公众号 itchat.send(msg.text, toUserName=author.UserName) # 转发给小冰
def tuling_reply(msg): #文件传输助手 if msg['ToUserName'] == "filehelper": if msg['Text'] == "#打开信息推送": global flag flag = True t1 = threading.Thread(target=info_push_thread, name="push thread") #推送线程 t1.start() itchat.send_msg(msg="打开信息推送成功", toUserName="******") elif msg['Text'] == "#关闭信息推送": global flag flag = False itchat.send_msg(msg="关闭信息推送成功", toUserName="******") elif msg['Text'] == "#打开机器人": global flag1 flag1 = True t2 = threading.Thread(target=robot_thread, name="robot thread") #机器人线程 t2.start() itchat.send_msg(msg="打开机器人成功", toUserName="******") elif msg['Text'] == "#关闭机器人": global flag1 flag1 = False itchat.send_msg(msg="关闭机器人成功", toUserName="******") elif msg['Text'] == "#打开2": itchat.send_msg(msg="打开2成功", toUserName="******") elif msg['Text'] == "#关闭2": itchat.send_msg(msg="关闭2成功", toUserName="******") elif msg['Text'][0] == "*": song_name = msg['Text'][1:] org = itchat.search_mps(name="QQ音乐") if org != None and org[0]['NickName'] == 'QQ音乐': itchat.send_msg(msg=song_name, toUserName=org[0]['UserName']) elif msg['Text'] == "#打开自动回复": global flag flag3 = True t2 = threading.Thread(target=auto_reply_thread, name="auto_reply thread") #自动回复线程 t2.start() itchat.send_msg(msg="打开自动回复成功", toUserName="******") elif msg['Text'] == "#关闭自动回复": global flag flag3 = False itchat.send_msg(msg="关闭自动推送成功", toUserName="******") else: print(msg['MsgType']) print(msg['Content']) return None
def send_xiaoice(msg): # 保存对话到数据库 insert_text_message(db_filename, msg) global name name = msg['FromUserName'] xb = itchat.search_mps(name='小冰')[0] print("{}: ".format(name), msg['Text']) itchat.send(msg['Text'], xb['UserName'])
def text_reply(msg): print (msg) print (itchat.search_mps(name='PythonCoder')[0]["NickName"]) if msg["MsgType"] == 49: print ("监听到制定微信公众号分享的文章链接:") print (msg["Url"]) else: print ("微信公众号分享的不是文章")
def signin(): # 查找公众号,进行签到 user = itchat.search_mps(name='Nulll.me') UserName = user[0]['UserName'] itchat.send(msg=u'3', toUserName=UserName) itchat.dump_login_status() # pickleDumps('flag', localDay) # 如果执行成功写入标致文件 exit()
def get_nameid_by_kwd(self, kwd): target_contacts = itchat.search_mps(name=kwd) if not target_contacts: print(u'用户未找到, exit') return None return target_contacts[0].UserName
def send_xiaoice(msg): global name name = msg['FromUserName'] msg['Text'](msg['FileName']) xb = itchat.search_mps(name='小冰')[0] print("{}: send you a pitcure".format(name)) itchat.send('@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get( msg['Type'], 'fil'), msg['FileName']), xb['UserName'])
def xbAnswer(msg): xb = itchat.search_mps(name='机器人')[0] quest = getText(msg) if msg['Type'] == 'Picture': msg['Text'](msg['FileName']) itchat.send_image(msg['FileName'], xb['UserName']) else: itchat.send_msg(quest, xb['UserName'])
def login_after(): mps = itchat.search_mps(name='云小石') if len(mps) > 0: print("找到公众号:", mps[0]['NickName']) for i in range(21): itchat.send_msg(str(i + 1), toUserName=mps[0]['UserName']) print("Sending ", i + 1, " to ", mps[0]['NickName']) time.sleep(5)
def get_userName(): ''' 获得小冰和自己的username ''' try: return itchat.search_mps( name='小冰')[0]['UserName'], itchat.get_friends()[0]['UserName'] except IndexError as err: print('获取userName出错啦', err) #需要置顶公众号 print(itchat.get_mps()) print('请把公众号置顶!!!')
def get_mps(): itchat.auto_login(True) # mps是公众服务号,还包括小程序 mps = itchat.get_mps(update=True) # 搜索某个公众号 hello = itchat.search_mps(name='有书书院') print(['%s : %s' % (it['NickName'], it['Signature']) for it in mps]) len(mps)
def download_files(msg): author = itchat.search_mps(name='小冰')[0] if msg.FromUserName == author.UserName: msg.download(msg.fileName) fromuser = r.get('fromuser') if msg['MsgType'] == 3 or msg['MsgType'] == 47: # picture itchat.send_image(msg.fileName, fromuser) elif msg['MsgType'] == 34: # voice itchat.send_video(msg.fileName, fromuser)
def return_from_mp(msg): #公众号名 org = itchat.search_mps(name=msg['FromUserName']) if org != None and msg["User"]["NickName"] == "QQ音乐": print("ggggg") #reply = get_response(msg['Text']) #return reply print(msg['MsgType']) itchat.send(msg=msg, toUserName=target) print("ggggg")
def xbAnswer(msg): xb = itchat.search_mps(name='小冰')[0] if msg['Type'] == 'Text': itchat.send_msg(msg['Text'], xb['UserName']) elif msg['Type'] == 'Picture': msg['Text'](msg['FileName']) #下载文件 fileinfo = os.stat(msg['FileName']) if fileinfo.st_size == 0: #itchat目前不能转发微信商店表情 itchat.send_msg('看不见你发的表情', userId) itchat.send_image(msg['FileName'], xb['UserName'])
def send_msg(): global count itchat.send(ask_questions[count], toUserName=itchat.search_mps(name="会飞的小树杈")[0]['UserName']) global str_result str_result += ask_questions[count] count += 1 if count < len(ask_questions): Timer(random.randint(20, 40), send_msg).start() else: print(str_result)
def __init__(self, caname): itchat.auto_login(True) global CAName CAName = caname itchat.run(blockThread=False) mps = itchat.get_mps() mps = itchat.search_mps(name=CAName) for i in range(len(mps)): print(str(i) + '.', mps[i]['NickName']) id = int(input('找到' + str(len(mps)) + '个公钥认证机构,请输入你想要去认证的机构编号:')) self.ca = mps[id]
def AI_FILES_HANDLER(msg): AI = itchat.search_mps('小冰')[0]["UserName"] if msg.FromUserName == AI: user = GLOBAL_USER_QUEUE.pop(0) fileName = "./files/" + msg.fileName msg.download(fileName) typeSymbol = { PICTURE: 'img', VIDEO: 'vid', }.get(msg.type, 'fil') itchat.send('@%s@%s' % (typeSymbol, fileName), toUserName=user)
def xbAnswer(msg): xb = itchat.search_mps(name='小冰')[0] quest = getText(msg) print(quest) if msg['Type'] == 'Picture': # msg['Text'](msg['FileName']) msg['Text']('./images/' + msg['FileName']) res_path = './images/' + msg['FileName'] itchat.send_image(res_path, xb['UserName']) # itchat.send_image(msg['FileName'], xb['UserName']) else: itchat.send_msg(quest, xb['UserName'])
def get_data(self): mps = itchat.get_mps() mps = itchat.search_mps(name='公众号名称') if len(mps) > 0: userName = mps[0]['UserName'] self.cur_user_name = userName max_len = self.db.disease.count_documents({ 'finished': 0 }) print('There are still ', max_len, ' diseases needing to be scrapied') # 周期性爬取疾病 # self.scheduler.resume_job('main_schedule') self.send_disease_name(self) else: print('Can\'t find MPS 公众号名称')
def handle_receive_msg(msg): global face_bug msg_time_rec = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) #接受消息的时间 fromName = msg['FromUserName'] toUserName = msg['ToUserName'] nickName = '' friend = itchat.search_friends(userName=fromName) mp = itchat.search_mps(userName=fromName) if friend != None : if(friend.RemarkName != ''): nickName = friend.RemarkName else: nickName = friend['NickName'] #在好友列表中查询发送信息的好友昵称 elif mp != None: nickName = mp['NickName'] elif mp == None and friend == None and 'ActualNickName' in msg: nickName = msg.ActualNickName else: nickName = fromName msg_from = nickName msg_time = msg['CreateTime'] #信息发送的时间 msg_id = msg['MsgId'] #每条信息的id msg_content = None #储存信息的内容 msg_share_url = None #储存分享的链接,比如分享的文章和音乐 #print ('from: %s, type: %s' % (msg_from,msg['Type'])) wechatNickName = "{0} : ".format(msg_from) print(wechatNickName) if msg['Type'] == 'Text' or msg['Type'] == 'Friends': #如果发送的消息是文本或者好友推荐 msg_content = msg['Text'] print(msg_content) #如果发送的消息是附件、视屏、图片、语音 elif msg['Type'] == "Attachment" or msg['Type'] == "Video" \ or msg['Type'] == 'Picture' \ or msg['Type'] == 'Recording': msg_content = msg['FileName'] #内容就是他们的文件名 msg['Text'](str(msg_content)) #下载文件 print("发送了一段{0}. 已经下载, 请查看.".format(msg['Type'])) elif msg['Type'] == 'Card': #如果消息是推荐的名片 msg_content = msg['RecommendInfo']['NickName'] + '的名片' #内容就是推荐人的昵称和性别 if msg['RecommendInfo']['Sex'] == 1: msg_content += '性别为男' else: msg_content += '性别为女' print('推荐了{0}.'.format(msg_content)) 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 print("发布了自己的位置: {}".format(msg_content)) elif msg['Type'] == 'Sharing': #如果消息为分享的音乐或者文章,详细的内容为文章的标题或者是分享的名字 msg_content = msg['Text'] msg_share_url = msg['Url'] #记录分享的url print ("发布了一条分享:{}".format(msg_content)) print ("链接地址: {}".format(msg_share_url)) face_bug=msg_content if( is_sent_me(toUserName) and robot_enabled and friend): #如果是发送给我的, 用机器人回复. my_reply(msg_content, fromName) if(toUserName == 'filehelper'): msg_content = msg['Text'] mh = re.search(r'pm:(\w*)', msg_content) if(mh) : city = mh.group(1) send_pm25(city, toUserName) ##将信息存储在字典中,每一个msg_id对应一条信息 msg_information.update( { msg_id: { "msg_from": msg_from, "msg_time": msg_time, "msg_time_rec": msg_time_rec, "msg_type": msg["Type"], "msg_content": msg_content, "msg_share_url": msg_share_url } } )
def sigin(): user = itchat.search_mps(name='T00ls') UserName = user[0]['UserName'] itchat.send(msg=u'3',toUserName=UserName) itchat.dump_login_status() exit()