def SaveMsg(msg): mytime = time.localtime() 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__()) # 创建可下载消息内容的存放文件夹,并将暂存在当前目录的文件移动到该文件中 if not os.path.exists("." + os.sep + "Revocation" + os.sep): os.mkdir("." + os.sep + "Revocation" + os.sep) msg_id, old_msg = GetOldMsg(msg) if old_msg: msg_send = GetSendMsg(old_msg, msg_time_touser) itchat.send(msg_send, toUserName='******') # 将撤回消息的通知以及细节发送到文件助手 itchat.send( '@%s@%s' % ('img' if old_msg['msg_type'] == 'Picture' else 'fil', "." + os.sep + "Revocation" + os.sep + old_msg["msg_content"]), 'filehelper') #发送小视频,文件 if (old_msg['msg_type'] == 'Attachment'): itchat.send_file( "." + os.sep + "Revocation" + os.sep + old_msg['msg_content'], 'filehelper') if (old_msg['msg_type'] == 'Video'): itchat.send_video( "." + os.sep + "Revocation" + os.sep + old_msg["msg_content"], 'filehelper') msg_dict.pop(msg_id)
def group_pic(msg): group = itchat.get_chatrooms(update=True, ) from_user = '' for g in group: if g['NickName'] == '来123': #从群中找到指定的群聊 from_group = g['UserName'] ChatRoom = itchat.update_chatroom(from_group, detailedMember=True) for menb in ChatRoom['MemberList']: if menb['NickName'] == "柒年": #从群成员列表找到用户,只转发他的消息 from_user = menb['UserName'] for j in group: if j['NickName'] == '来': #把消息发到这个群 to_group = j['UserName'] if msg['ToUserName'] == from_group: if msg['FromUserName'] == from_user: msg['Text'](msg['FileName']) if msg['Type'] == "Picture": itchat.send_image( fileDir=msg['FileName'], toUserName=to_group) if msg['Type'] == "Recording": itchat.send_file( fileDir=msg['FileName'], toUserName=to_group) if msg['Type'] == "Attachment": itchat.send_file( fileDir=msg['FileName'], toUserName=to_group) if msg['Type'] == "Video": itchat.send_video( fileDir=msg['FileName'], toUserName=to_group)
def to_laopo(_type, msg, kivy, _path=None): if msg.User.UserName[0:2] != '@@': return try: _group_name = msg.User.NickName except: _group_name = "" if _group_name in [ '盛世名门群主楼长群', '名门团队', '吃花生🥜', '宏城汇新希望群(物业交流)', '盛世名门业主代表沟通交流群', '宏城汇商场事宜沟通群', '商场围闭沟通群', '测试群' ]: _user_name = msg.ActualNickName if _type == TEXT: itchat.send('[' + _group_name + ']' + _user_name + ':\n' + msg.text, toUserName=kivy.laopo_username) elif _type == PICTURE: itchat.send('[' + _group_name + ']' + _user_name + ':', toUserName=kivy.laopo_username) itchat.send_image(_path, toUserName=kivy.laopo_username) elif _type in [ATTACHMENT, RECORDING]: itchat.send('[' + _group_name + ']' + _user_name + ':', toUserName=kivy.laopo_username) itchat.send_file(_path, toUserName=kivy.laopo_username) elif _type in [VIDEO]: itchat.send('[' + _group_name + ']' + _user_name + ':', toUserName=kivy.laopo_username) itchat.send_video(_path, toUserName=kivy.laopo_username) pass
def file_reply(msg): is_group = False target = msg['User']['UserName'] msg.download('./FileCache/' + msg.fileName) if is_group: sender_name = msg['ActualNickName'] if msg[ 'ActualNickName'] != '' else msg['User']['Self']['NickName'] # 消息来自于哪个群聊 group_name = msg['User']['NickName'] sender_name = '{} {}'.format(group_name, sender_name) else: sender_name = msg['User']['NickName'] if 'NickName' in msg[ 'User'] else 'filehelper' if msg['Type'] == 'Picture': itchat.send_msg('{} send picture: '.format(sender_name), target) itchat.send_image('./FileCache/' + msg.fileName, target) elif msg['Type'] == 'Recording': msg.user.send('%s : %s' % (msg.type, msg.text)) itchat.send_msg('{} send recording: '.format(sender_name), target) itchat.send_file('./FileCache/' + msg.fileName, target) elif msg['Type'] == 'Video': itchat.send_msg('{} send recording: '.format(sender_name), target) itchat.send_video('./FileCache/' + msg.fileName, target) else: itchat.send_msg('{} send file: '.format(sender_name), target) itchat.send_file('./FileCache/' + msg.fileName, target)
def video_reply(msg): # msg['Text']('./videos/' + msg['FileName']) User = itchat.search_friends(userName=msg['FromUserName']) if User['RemarkName'] == '': NickName = User['NickName'] else: NickName = '%s(%s)' % (User['NickName'], User['RemarkName']) print( '------------------------------------------------------------------------------' ) print('%s给您发送了一个视频,已经存入videos目录,文件名:%s' % (NickName, msg['FileName'])) print('AI帮您回复% s默认表情default.gif' % NickName) print( '------------------------------------------------------------------------------' ) path_list = return_video_path() res_path = './videos/%s' % (path_list[random.randint( 0, (len(path_list) - 1))]) if not replied.get(md5(msg['FromUserName'] + '_VIDEO')): # 当消息不是由自己发出的时候 replied[md5(msg['FromUserName'] + '_VIDEO')] = 1 return u"[陈主人暂时不在.继续发送视频.助理Neo将会发送一些小视频给你.Don't be too serious.] " # 回复给好友 elif replied[md5(msg['FromUserName'] + '_VIDEO')] < MAX_LIMIT_VIDEO: replied[md5(msg['FromUserName'] + '_VIDEO')] += 1 itchat.send_video(res_path, msg['FromUserName']) elif replied[md5(msg['FromUserName'] + '_VIDEO')] > MAX_LIMIT_VIDEO: pass else: replied[md5(msg['FromUserName'] + '_VIDEO')] += 1 return u"[福利 are not free.发送红包后继续.Have a nice day & See you!] "
def AIreply(msg): print("收到") if msg['Type'] == 'Picture': msg['Text'](msg['FileName']) itchat.send_image(msg['FileName'], toUserName=userId) os.remove(msg['FileName']) elif msg['Type'] == 'Video': msg['Text'](msg['FileName']) itchat.send_video(msg['FileName'], toUserName=userId) os.remove(msg['FileName']) elif msg['Type'] == 'Recording': msg['Text'](msg['FileName']) #保存为mp3 itchat.send_file(msg['FileName'], toUserName=userId) os.remove(msg['FileName']) elif msg['Type'] == 'Text': if ('http' in msg['Text']): dealLink(msg) itchat.send_msg(msg['Text'], toUserName=userId) elif msg['Type'] == 'Sharing': print("音乐") message = re.findall('<url>.*</url>', msg['Content'])[0][5:-6] if (message[0:4] == "http"): itchat.send(message, toUserName=userId) else: pass
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 file_reply(msg): global private_users, admin_users get_config_users() sender_name = msg['User']['NickName'] if 'NickName' in msg['User'] else 'filehelper' msg.download('./filecache/' + msg.fileName) logger.logger.info('[file_reply] Files recieved from:{}'.format(sender_name)) for user in private_users: if user in sender_name: receiver = 'admin_users' break else: receiver = 'filehelper' if receiver == 'filehelper': logger.logger.info('[file_reply] Files forward to:{}'.format(receiver)) if msg.type == 'PICTURE': itchat.send_image('./filecache/' + msg.fileName, receiver) elif msg.type == 'VIDEO': itchat.send_video('./filecache/' + msg.fileName, receiver) else: itchat.send_file('./filecache/' + msg.fileName, receiver) elif receiver == 'admin_users': logger.logger.info('[file_reply] Files forward to:{}'.format(receiver)) for admin_user in admin_users: if msg.type == 'PICTURE': itchat.send_image('./filecache/' + msg.fileName, admin_users[admin_user]) elif msg.type == 'VIDEO': itchat.send_video('./filecache/' + msg.fileName, admin_users[admin_user]) else: itchat.send_file('./filecache/' + msg.fileName, admin_users[admin_user])
def revoke_msg(msg): users = itchat.search_friends(name='Vson') userName = users[0]['UserName'] if re.search(r"\<\!\[CDATA\[.*撤回了一条消息\]\]\>", msg['Content']) is not None: old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) old_msg = rec_msg_dict.get(old_msg_id, {}) print('有人撤回了一条信息:' + str(old_msg.get('msg_from_user')) + ':' + str(old_msg.get('msg_content'))) #发送给自己的账号 string1 = str(old_msg.get('msg_from_user')) + ':' + str( old_msg.get('msg_content')) itchat.send(string1, toUserName=userName) # 判断文msg_content是否存在,不存在说明可能是 if os.path.exists( os.path.join(rec_tmp_dir, str(old_msg.get('msg_content')))): if old_msg.get('msg_type') == 'Picture': itchat.send_image(os.path.join(rec_tmp_dir, old_msg.get('msg_content')), toUserName="******") string1 = str(old_msg.get('msg_from_user')) + ':' + str( old_msg.get('msg_content')) itchat.send(string1, toUserName=userName) elif old_msg.get('msg_type') == 'Video': itchat.send_video(os.path.join(rec_tmp_dir, old_msg.get('msg_content')), toUserName="******") elif old_msg.get('msg_type') == 'Attachment' \ or old_msg.get('msg_type') == 'Recording': itchat.send_file(os.path.join(rec_tmp_dir, old_msg.get('msg_content')), toUserName="******")
def handle_recall_friendchat(msg): if '撤回了一条消息' in msg['Content']: old_msg_id = re.search(r'\<msgid\>(.*?)\<\/msgid\>', msg['Content']).group(1) old_msg = msg_info[old_msg_id] print(old_msg_id) print(old_msg) itchat.send_msg('%s撤回了一条%s消息[%s],内容为:%s' % (old_msg['msg_from'], old_msg['msg_type'], old_msg['msg_recv_time'], old_msg['msg_content']), toUserName='******') # 分享链接还需要发送地址 if old_msg['msg_type'] == ' Sharing': itchat.send_msg('->%s' % old_msg['msg_url'], toUserName='******') # 多媒体内容还需要发送文件 if old_msg['msg_type'] == 'Recording' or old_msg[ 'msg_type'] == 'Attachment': itchat.send_file(rev_tmp_dir + old_msg['msg_content'], toUserName='******') # 发送自动下载的语音或附件 if old_msg['msg_type'] == 'Picture': itchat.send_image(rev_tmp_dir + old_msg['msg_content'], toUserName='******') # 发送自动下载的照片 if old_msg['msg_type'] == 'Video': itchat.send_video(rev_tmp_dir + old_msg['msg_content'], toUserName='******') # 发送自动下载的视频 msg_info.pop(old_msg_id)
def revoke_msg(msg): if re.search(r"\<\!\[CDATA\[.*撤回了一条消息\]\]\>", msg['Content']) is not None: old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) old_msg = rec_msg_dict.get(old_msg_id, {}) # 先发送一条文字信息 itchat.send_msg(str( old_msg.get('msg_from_user') + "撤回了一条信息:" + old_msg.get('msg_content')), toUserName="******") # 判断文msg_content是否存在,不存在说明可能是 if os.path.exists(os.path.join(rec_tmp_dir, old_msg.get('msg_content'))): if old_msg.get('msg_type') == 'Picture': itchat.send_image(os.path.join(rec_tmp_dir, old_msg.get('msg_content')), toUserName="******") elif old_msg.get('msg_type') == 'Video': itchat.send_video(os.path.join(rec_tmp_dir, old_msg.get('msg_content')), toUserName="******") elif old_msg.get('msg_type') == 'Attachment' \ or old_msg.get('msg_type') == 'Recording': itchat.send_file(os.path.join(rec_tmp_dir, old_msg.get('msg_content')), toUserName="******") print(msg)
def SendRevokeMsg(self, msg): """ 把撤回的消息发送到文件助手 """ msg_id = self.IsRevokeMsg(msg) #获取撤回消息的ID msg_content = self.msg_store.get(msg_id, None) #获取撤回消息的内容 mytime = time.localtime() msg_time = "%s/%s/%s %s:%s:%s" % ( mytime.tm_year.__str__(), mytime.tm_mon.__str__(), mytime.tm_mday.__str__(), mytime.tm_hour.__str__(),mytime.tm_min.__str__(), mytime.tm_sec.__str__()) if (msg_id != None) and (msg_content != None): sendMsg = "" if msg_content['msg_group']: sendMsg += "群聊%s中"%(msg_content['msg_group']) sendMsg += "'%s'"%(msg_content['msg_from']) sendMsg += "在%s撤回了一条消息:\n"%(msg_time) if msg_content['msg_type'] == 'Sharing': sendMsg += "%s%s" % (msg_content['msg_url'], "\n\n") sendMsg += "%s"%(msg_content['msg_content']) itchat.send(sendMsg, toUserName = '******') if msg_content['msg_type'] == 'Picture': itchat.send_image(".\\Cache\\" + msg_content['msg_content'], toUserName = '******') elif msg_content['msg_type'] == 'Video': itchat.send_video(".\\Cache\\" + msg_content['msg_content'], toUserName = '******') elif (msg_content['msg_type'] == 'Attachment') or \ (msg_content['msg_type'] == 'Recording'): itchat.send_file(".\\Cache\\" + msg_content['msg_content'], toUserName = '******') self.msg_store.pop(msg_id) if os.path.exists(".\\Cache\\" + msg_content['msg_content']): os.remove(".\\Cache\\" + msg_content['msg_content'])
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 msg_relay(msg, relay_to): ''' 群: 群名称:msg.User.NickName 群成员有备注:msg.ActualNickName 个人: 有备注:msg.User.RemarkName 没有备注:msg.User.NickName ''' for key, value in relay_to.items(): if value['replay_list_type'] == 'group': if msg.User.UserName[0:2] != '@@': return group_name = '' user_name = '' try: group_name = msg.User.NickName except: pass if group_name in value['replay_list']: global DATA_FILE_PATH user_name = msg.ActualNickName if msg.type == TEXT: itchat.send('[%s]%s: \n%s' % (group_name, user_name, msg.text), toUserName=value['username']) elif msg.type == PICTURE: temp_path = DATA_FILE_PATH + 'temp/' + msg.fileName msg.download(temp_path) itchat.send('[%s]%s:' % (group_name, user_name), toUserName=value['username']) itchat.send_image(temp_path, toUserName=value['username']) os.remove(temp_path) elif msg.type in [ATTACHMENT, RECORDING]: temp_path = DATA_FILE_PATH + 'temp/' + msg.fileName msg.download(temp_path) itchat.send('[%s]%s:' % (group_name, user_name), toUserName=value['username']) itchat.send_file(temp_path, toUserName=value['username']) os.remove(temp_path) elif msg.type == VIDEO: temp_path = DATA_FILE_PATH + 'temp/' + msg.fileName msg.download(temp_path) itchat.send('[%s]%s:' % (group_name, user_name), toUserName=value['username']) itchat.send_video(temp_path, toUserName=value['username']) os.remove(temp_path) elif value['replay_list_type'] == 'friend': if msg.User.UserName[0:2] == '@@': return
def text_reply(msg): # print(msg) flag = 0 # 发送内容 message = msg['Text'] # 接收者 toName = msg['ToUserName'] print(toName) itchat.send_video('要发送的视频路径和视频名',"filehelper")
def reply_msg(msg): global msgReply if msg['Type'] != "Text" : if not msg['FromUserName'] == myUserName: msg['Text']("tmp/"+msg['FileName']) itchat.send_msg(u'%s 发来: \n%s' %(msg['User']['NickName'],msg['FileName']), toUserName=userName) if msg['Type'] == "Picture": itchat.send_image("tmp/"+msg['FileName'], toUserName=userName) elif msg['Type'] == "Video": itchat.send_video("tmp/"+msg['FileName'], toUserName=userName) else: itchat.send_file("tmp/"+msg['FileName'], toUserName=userName) return if 'start' in msg['Text'] and msg['FromUserName']==userName: msgReply=True itchat.send_msg(u'自动回复已开启', toUserName=userName) return elif 'stop' in msg['Text'] and msg['FromUserName']==userName: msgReply=False itchat.send_msg(u'自动回复已关闭', toUserName=userName) return elif "order" in msg['Text'] and msg['FromUserName'] == orderuserName: itchat.send_msg(u'可点菜单: \n' , toUserName=orderuserName) return elif 'reply' in msg['Text'] and msg['FromUserName']==userName: theEnd=msg['Text'].split(" ")[1] theNr=msg['Text'].split(" ")[2] ReplyUsers=itchat.search_friends(name=theEnd) ReplyName=ReplyUsers[0]['UserName'] itchat.send_msg(theNr,toUserName=ReplyName) elif msgReply == False : if not msg['FromUserName'] == myUserName: itchat.send_msg(u'%s 发来: \n%s' %(msg['User']['NickName'],msg['Text']), toUserName=userName) else: api_url = 'http://openapi.tuling123.com/openapi/api/v2' data = { "reqType": 0, "perception": { "inputText": { "text": str(msg['Content'].encode('utf8')) } }, "userInfo": { "apiKey": "yourkey", "userId": "123" } } headers = { 'Content-Type': 'application/json', 'Host': 'openapi.tuling123.com', 'User-Agent': 'Mozilla/5.0 (Wi`ndows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3486.0 ' 'Safari/537.36 ' } result = rq.post(api_url, headers=headers, json=data).json() itchat.send_msg(result['results'][0]['values']['text'], msg['FromUserName'])
def information(msg): # 永不监听自己的消息 if msg['FromUserName'] == selfId: return # 这里如果这里的msg['Content']中包含消息撤回和id,就执行下面的语句 if '撤回了一条消息' in msg['Content']: # 在返回的content查找撤回的消息的id old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) # 获取到消息原文 old_msg = msg_information.get(old_msg_id) logging.info('监听到一条撤回的消息--> ' + old_msg['summary']) itchat.send('监听到一条撤回的消息-->\n' + old_msg['summary'], toUserName='******') # 发送撤回的消息 if old_msg['msg_type'] == 'Text': msg_body = old_msg.get('msg_from') + " 撤回了一条消息:\n" + old_msg.get( 'msg_time_rec') + "\n" + old_msg.get('msg_content') itchat.send(msg_body, old_msg['FromUserName']) elif old_msg['msg_type'] == 'Picture': msg_body = old_msg.get('msg_from') + " 撤回了一张图片:\n" + old_msg.get( 'msg_time_rec') + "\n" + old_msg.get('msg_content') itchat.send(msg_body, old_msg['FromUserName']) itchat.send_image('files/' + old_msg.get('msg_content'), old_msg['FromUserName']) elif old_msg['msg_type'] == 'Video': msg_body = old_msg.get('msg_from') + " 撤回了一个视频:\n" + old_msg.get( 'msg_time_rec') + "\n" + old_msg.get('msg_content') itchat.send(msg_body, old_msg['FromUserName']) itchat.send_video('files/' + old_msg.get('msg_content'), old_msg['FromUserName']) elif old_msg['msg_type'] == 'Recording': msg_body = old_msg.get('msg_from') + " 撤回了一条语音:\n" + old_msg.get( 'msg_time_rec') + "\n" + old_msg.get('msg_content') itchat.send(msg_body, old_msg['FromUserName']) itchat.send_file('files/' + old_msg.get('msg_content'), old_msg['FromUserName']) elif old_msg['msg_type'] == 'Attachment': msg_body = old_msg.get('msg_from') + " 撤回了一张图片:\n" + old_msg.get( 'msg_time_rec') + "\n" + old_msg.get('msg_content') if check_contain_chinese(old_msg.get('msg_content')): msg_body = msg_body + old_msg.get( 'msg_content') + "\n因文件名含中文,无法重新发送" itchat.send(msg_body, old_msg['FromUserName']) itchat.send_file(u'files/' + old_msg.get('msg_content'), old_msg['FromUserName']) else: msg_body = old_msg.get( 'msg_from') + " 撤回了一条消息,该消息类型暂时不支持:\n" + old_msg.get( 'msg_time_rec') + "\n" + old_msg.get('msg_content') itchat.send(msg_body, old_msg['FromUserName'])
def revoke_msg(msg): nickName = str(itchat.get_friends(update=True)[1]['NickName']) is_revoke = re.search(r'(.*)CDATA(.*)撤回了一条消息]]>', str(msg['Content'])) if is_revoke is not None: old_msg_id = re.search(r'<msgid>(.*)</msgid>', is_revoke.group(1)).group(1) old_msg = rec_msg_dict.get(old_msg_id, {}) # 先发送一条文字信息 type_obj = { 'Text': "一条文字信息", 'Picture': "一张图片", 'Recording': "一段语音", 'Video': "一个视频", 'Attachment': "一个文件", 'Sharing': "一个分享", 'Map': "一个位置信息", 'Card': "一个名片分享", } key = str(old_msg.get('msg_type')) revoke_file_type = type_obj.get(key, '一条文字信息') if str(old_msg.get('msg_type')) == 'Sharing': sharing_appid = re.match(r'(.*)appid="(.*)" sdkver', str(old_msg.get('msg_content'))).group(2) if not sharing_appid: revoke_file_type = '一个小程序' old_msg['msg_content'] = re.match(r'(.*)<sourcedisplayname>(.*)</sourcedisplayname>', str(old_msg.get('msg_content'))).group(2) + "," + '\r\n描述:' + re.findall(r"<title>(.+?)</title>", str(old_msg.get('msg_content')))[0] else: sharing_from = re.match(r'(.*)<appname>(.*)</appname></appinfo>', str(old_msg.get('msg_content'))).group(2) sharing_content = re.findall(r"<title>(.+?)</title>", str(old_msg.get('msg_content')))[0] sharing_url = re.match(r'(.*)<url>(.*)</url><lowurl', str(old_msg.get('msg_content'))).group(2) old_msg['msg_content'] = sharing_content + '\r\n链接:' + sharing_url + '\r\n来源:' + sharing_from if old_msg.get('msg_from_user') != u'\uabed': if str(old_msg.get('msg_type')) == 'Sharing' \ or str(old_msg.get('msg_type')) == 'Map' \ or str(old_msg.get('msg_type')) == 'Card': itchat.send_msg(str(old_msg.get('msg_from_user')) + "撤回了" + str(revoke_file_type) + ": " + str(old_msg.get('msg_content')), msg['FromUserName']) itchat.send_msg(str(old_msg.get('msg_from_user')) + "撤回了" + str(revoke_file_type) + ": " + str(old_msg.get('msg_content')), toUserName="******") else: itchat.send_msg(str(old_msg.get('msg_from_user')) + "撤回了" + str(revoke_file_type), msg['FromUserName']) itchat.send_msg(str(old_msg.get('msg_from_user')) + "撤回了" + str(revoke_file_type) + ": " + str(old_msg.get('msg_content')), toUserName="******") # 判断文msg_content是否存在,不存在说明可能是 if os.path.exists(os.path.join(rec_tmp_dir, old_msg.get('msg_content'))): if old_msg.get('msg_type') == 'Picture': itchat.send_image(os.path.join(rec_tmp_dir, old_msg.get('msg_content')), toUserName="******") elif old_msg.get('msg_type') == 'Video': itchat.send_video(os.path.join(rec_tmp_dir, old_msg.get('msg_content')), toUserName="******") elif old_msg.get('msg_type') == 'Attachment' \ or old_msg.get('msg_type') == 'Recording': itchat.send_file(os.path.join(rec_tmp_dir, old_msg.get('msg_content')), toUserName="******")
def video_recieved(msg): global Video_Limit if msg['FromUserName'] not in GamePeople: itchat.send_msg('您不在聊天中,视频将不会被接收.', toUserName=msg['FromUserName']) else: if Video_Limit == True: itchat.send_msg('#[系统提示]当前服务器人数较多或在维护中,视频将不会被转发.', toUserName=msg['FromUserName']) return msg.download(msg.fileName) itchat.send_msg('对方发送了一条视频消息,正在处理,请稍等...', toUserName=PeopleAll[msg['FromUserName']]['ToUser']) itchat.send_video(msg.fileName, toUserName=PeopleAll[msg['FromUserName']]['ToUser'])
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) if ("新闻" in msg['Text']): news = tulin.askTuling(msg['Text']) if (news != None): itchat.send_msg(news['text'], toUserName=msg['FromUserName']) itchat.send_msg(news['news']['name'], toUserName=msg['FromUserName']) r = requests.get("http:" + news['news']['icon']) with open("tmp.jpg", "wb") as f: f.write(r.content) itchat.send_image("tmp.jpg", toUserName=msg['FromUserName']) os.remove("tmp.jpg") itchat.send_msg("链接 : " + news['news']['detailurl'], toUserName=msg['FromUserName']) else: itchat.send_msg(msg['Text'], toUserName=xb['UserName']) elif ("图片" in msg['Text']): pic = tulin.askTuling(msg['Text']) if (pic != None): itchat.send_msg(pic['text'], toUserName=msg['FromUserName']) itchat.send_msg("链接 : " + pic['url'], toUserName=msg['FromUserName']) else: itchat.send_msg(msg['Text'], toUserName=xb['UserName']) else: itchat.send_msg(msg['Text'], toUserName=xb['UserName'])
def my_send_msg(msg, user): msg_type = msg['Type'] if msg_type == 'Text': itchat.send(msg['Content'], user) elif msg_type == 'Picture': msg['Text'](msg['FileName']) itchat.send_image(msg['FileName'], user) elif msg_type == 'Recording' or msg_type == 'Attachment': msg['Text'](msg['FileName']) itchat.send_file(msg['FileName'], user) elif msg_type == 'Video': msg['Type'](msg['FileName']) itchat.send_video(msg['FileName'], user) else: itchat.send(msg['Contetn'], user)
def after(): user_info = itchat.search_friends(name='培杰') if len(user_info) > 0: # 拿到用户名 user_name = user_info[0]['UserName'] # 发送文字信息 itchat.send_msg('培杰你好啊!', user_name) # 发送图片 time.sleep(10) itchat.send_image('cat.jpg', user_name) # 发送文件 time.sleep(10) itchat.send_file('19_2.py', user_name) # 发送视频 time.sleep(10) itchat.send_video('sport.mp4', user_name)
def sender(counter=0): if counter < len(sentences): if(sentences[counter]['type'] == 'string'): itchat.send(sentences[counter]['content'], toUserName=toUserName) elif (sentences[counter]['type'] == 'image'): itchat.send_image(sentences[counter]['content'], toUserName=toUserName) elif (sentences[counter]['type'] == 'video'): itchat.send_video(sentences[counter]['content'], toUserName=toUserName) else: pass counter += 1 else: itchat.send('finish', toUserName=toUserName) return t = threading.Timer(1, sender, (counter,)) t.start()
def send(self, name, connut): # 发送消息 # 获取对方的信息 users = itchat.search_friends(name=name) if not users: print("没有找到此人") return # 获取`UserName`,用于发送消息 userName = users[0]['UserName'] if connut.endswith(".txt"): itchat.send_file(u'%s' % connut, userName) elif connut.endswith(".png") or connut.endswith(".jpg"): itchat.send_image(u'%s' % connut, userName) elif connut.endswith(".mp4"): itchat.send_video(u'%s' % connut, userName) else: itchat.send(u'%s' % connut, userName)
def send_msg_helper(msg): global face_bug # 这里如果这里的msg['Content']中包含消息撤回和id,就执行下面的语句 if re.search(r"\<\!\[CDATA\[.*撤回了一条消息\]\]\>", msg['Content']) is not None: # 获取消息的id old_msg_id = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group( 1) # 在返回的content查找撤回的消息的id old_msg = rec_msg_dict.get(old_msg_id, {}) # 得到消息 if len(old_msg_id) < 11: # 如果发送的是表情包 itchat.send_file(rev_tmp_dir + face_bug, toUserName='******') #os.remove(rev_tmp_dir + face_bug) else: msg_body = "告诉你一个秘密~" + "\n" \ + old_msg.get('msg_from') + " 撤回了 " + old_msg.get("msg_type") + " 消息" + "\n" \ + old_msg.get('msg_time_rec') + "\n" \ + "撤回了什么 ⇣" + "\n" \ + r"" + old_msg.get('msg_content') # 如果是分享存在链接 if old_msg['msg_type'] == "Sharing": msg_body += "\n就是这个链接➣ " + old_msg.get('msg_share_url') print(msg_body) # 将撤回消息发送到文件助手 itchat.send(msg_body, toUserName='******') # 有文件的话也要将文件发送回去 # 判断文msg_content是否存在,不存在说明可能是 if os.path.exists(os.path.join(rec_tmp_dir, old_msg['msg_content'])): if old_msg["msg_type"] == "Picture": itchat.send_image(os.path.join(rec_tmp_dir, old_msg['msg_content']), toUserName="******") elif old_msg["msg_type"] == "Video": itchat.send_video(os.path.join(rec_tmp_dir, old_msg['msg_content']), toUserName="******") elif old_msg["msg_type"] == "Attachment" \ or old_msg["msg_type"] == "Recording": itchat.send_file(os.path.join(rec_tmp_dir, old_msg['msg_content']), toUserName="******")
def document_handler(self, msg): self.bot.sendMessage( config.telegramId, f"File received, forwarding to {contact_shower(self.usr_nick, self.usr_remark)}" ) file_name = msg['document']['file_name'] file_id = msg['document']['file_id'] self.bot.download_file(file_id, file_name) print("File received, forwarding...") if msg['document']['mime_type'][:5] == 'video': itchat.send_video(file_name, self.usr_name) elif msg['document']['mime_type'][:5] == 'image': itchat.send_image(file_name, self.usr_name) else: itchat.send_file(file_name, self.usr_name) os.remove(file_name) self.bot.sendMessage(config.telegramId, "File forwarding complete!") print("File forwarded")
def group_file_reply(msg): global private_groups get_config_users() msg.download('./filecache/' + msg.fileName) # 消息来自于哪个群聊 group_name = msg['User']['NickName'] logger.logger.info('[group_file_reply] Files recieved from:{}'.format(group_name)) if group_name in private_groups.keys(): group_id = private_groups[group_name] logger.logger.info('[group_file_reply] Files forward to:{}'.format('filehelper')) if msg.type == 'PICTURE': # itchat.send_image('./filecache/' + msg.fileName, group_id) itchat.send_image('./filecache/' + msg.fileName, 'filehelper') elif msg.type == 'VIDEO': # itchat.send_video('./filecache/' + msg.fileName, group_id) itchat.send_video('./filecache/' + msg.fileName, 'filehelper') else: # itchat.send_file('./filecache/' + msg.fileName, group_id) itchat.send_file('./filecache/' + msg.fileName, 'filehelper')
def map_reply(msg): if msg['FromUserName'] == callback.xiaoice: try: if msg['Type'] == 'Picture': msg['Text'](msg['FileName']) itchat.send_image(msg['FileName'], callback.msg.FromUserName) elif msg['Type'] == 'Text': itchat.send_msg(msg.text, callback.msg.FromUserName) elif msg['Type'] == 'VIDEO': logger.warning(msg) msg['Text'](msg['FileName']) itchat.send_video( msg['FileName'], callback.msg.FromUserName) else: logger.warning(msg) msg['Text'](msg['FileName']) itchat.send_video(msg['FileName'], callback.msg.FromUserName) callback.msg = None except Exception as e: logger.error('Failed to reply map info {}'.format(e))
def note(msg): # print(msg) # 判断里面有没有出现replacemsg,如果有的话就说明是撤回 if re.search(r"<replacemsg>", msg['Content']) != None: try: print("Recalled!") # 把撤回的消息序号返回,但是不是oldmsgid的序号(这个oldmsgid不知道是什么东西) oldMsg = re.search("\<msgid\>(.*?)\<\/msgid\>", msg['Content']).group(1) sendMsg = msgRec[oldMsg]['FromName']; sendMsg += " 撤回了一条 " + msgRec[oldMsg]['Type'] + " 消息:" if msgRec[oldMsg]['Type'] == "Text" or msgRec[oldMsg]['Type'] == "Friends": sendMsg += msgRec[oldMsg]['Text'] itchat.send(sendMsg, toUserName='******') elif msgRec[oldMsg]['Type'] == "Picture": itchat.send(sendMsg, toUserName='******') itchat.send_image(msgRec[oldMsg]['FileName'], toUserName='******') elif msgRec[oldMsg]['Type'] == "Video": itchat.send(sendMsg, toUserName='******') itchat.send_video(msgRec[oldMsg]['FileName'], toUserName='******') elif msgRec[oldMsg]['Type'] == "Recording": itchat.send(sendMsg, toUserName='******') itchat.send_file(msgRec[oldMsg]['FileName'], toUserName='******') elif msgRec[oldMsg]['Type'] == "File": itchat.send(sendMsg, toUserName='******') itchat.send_file(msgRec[oldMsg]['FileName'], toUserName='******') elif msgRec[oldMsg]['Type'] == "Card": sendMsg += "名字为:" + msgRec[oldMsg]['RecommendInfo']['NickName'] itchat.send(sendMsg, toUserName='******') elif msgRec[oldMsg]['Type'] == "Map": x, y, location = re.search("<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msgRec[oldMsg]['OriContent']).group(1, 2, 3) if location is None: sendMsg += " x: " + x.__str__() + " y: " + y.__str__() else: sendMsg += " x: " + x.__str__() + " y: " + y.__str__() + " loc: " + location itchat.send(sendMsg, toUserName='******') elif msgRec[oldMsg]['Type'] == 'Sharing': sendMsg += "内容:" + msgRec[oldMsg]['Text'] + "连接:" + msgRec[oldMsg]['Url'] except: print("Error!") else: # 其实我也不知道除了撤回还有什么别的……抢红包什么的都不支持啊,就直接发出来吧,总之有记录 itchat.send("不支持的消息", toUserName='******')
def send_video(file_dir=None, to_user_name=None, media_id=None): ''' send video for options - file_dir: dir for file ready for upload - if media_id is set, it's unnecessary to set file_dir - media_id: media_id for file. - if set, file will not be uploaded twice - to_user_name: 'user_name' key of friend dict it is defined in components/messages.py ''' return itchat.send_video(file_dir, to_user_name)