def exit_callback(self): if self.stop_polling: return msg = EFBMsg(self) msg.source = MsgSource.System msg.origin = { 'name': '%s System' % self.channel_name, 'alias': '%s System' % self.channel_name, 'uid': -1 } msg.text = "WeChat server logged out the user." msg.type = MsgType.Text on_log_out = self._flag("on_log_out", "command") on_log_out = on_log_out if on_log_out in ("command", "idle", "reauth") else "command" if on_log_out == "command": msg.type = MsgType.Command msg.attributes = { "commands": [{ "name": "Log in", "callable": "reauth", "args": [], "kwargs": { "command": True } }] } elif on_log_out == "reauth": if self._flag("qr_reload", "master_qr_code") == "console_qr_code": msg.text += "\nPlease visit your console or log for QR code and further instructions." self.reauth() self.queue.put(msg)
def master_qr_code(self, uuid, status, qrcode): status = int(status) msg = EFBMsg(self) msg.type = MsgType.Text msg.source = MsgSource.System msg.origin = { 'name': '%s Auth' % self.channel_name, 'alias': '%s Auth' % self.channel_name, 'uid': -1 } if status == 201: msg.type = MsgType.Text msg.text = 'Tap "Confirm" to continue.' elif status == 200: msg.type = MsgType.Text msg.text = "Successfully authenticated." elif uuid != self.qr_uuid: msg.type = MsgType.Image path = os.path.join("storage", self.channel_id) if not os.path.exists(path): os.makedirs(path) path = os.path.join(path, 'QR-%s.jpg' % int(time.time())) self.logger.debug("master_qr_code file path: %s", path) qr_url = "https://login.weixin.qq.com/l/" + uuid QRCode(qr_url).png(path, scale=10) msg.text = 'Scan this QR Code with WeChat to continue.' msg.path = path msg.file = open(path, 'rb') msg.mime = 'image/jpeg' if status in (200, 201) or uuid != self.qr_uuid: self.queue.put(msg) self.qr_uuid = uuid
def fileMsg(self, msg, isGroupChat=False): mobj = EFBMsg(self) mobj.type = MsgType.File mobj.path, mobj.mime = self.save_file(msg, mobj.type) mobj.text = msg['FileName'] mobj.file = open(mobj.path, "rb") return mobj
def wechat_friend_msg(self, msg): mobj = EFBMsg(self) txt = ("Friend request: {NickName}\n" "From: {Province}, {City}\n" "QQ: {QQNum}\n" "ID: {Alias}\n" "Signature: {Signature}\n" "Gender: {Sex}") tdict = msg['Text'].copy() tdict.update(msg['Text']['userInfo']) txt = txt.format(**tdict) mobj.text = txt mobj.type = MsgType.Command mobj.attributes = { "commands": [{ "name": "Send friend request", "callable": "add_friend", "args": [], "kwargs": { "userName": msg['Text']['userInfo']['UserName'], "status": 3, "ticket": msg['Ticket'] } }] } return mobj
def wechat_video_msg(self, msg): mobj = EFBMsg(self) mobj.path, mobj.mime = self.save_file(msg, MsgType.Video) mobj.type = MsgType.Video mobj.text = None mobj.file = open(mobj.path, "rb") return mobj
def wechat_location_msg(self, msg): mobj = EFBMsg(self) mobj.text = msg['Content'].split('\n')[0][:-1] loc = re.search("=-?([0-9.]+),-?([0-9.]+)", msg['Url']).groups() mobj.attributes = {"longitude": float(loc[1]), "latitude": float(loc[0])} mobj.type = MsgType.Location return mobj
def wechat_raw_link_msg(self, msg, title, description, image, url): mobj = EFBMsg(self) if url: mobj.type = MsgType.Link mobj.attributes = { "title": title, "description": description, "image": image, "url": url } else: mobj.type = MsgType.Text mobj.text = "%s\n%s" % (title, description) if image: mobj.text += "\n\n%s" % image return mobj
def videoMsg(self, msg, isGroupChat=False): mobj = EFBMsg(self) mobj.path, mobj.mime = self.save_file(msg, MsgType.Video) mobj.type = MsgType.Video mobj.text = None mobj.file = open(mobj.path, "rb") return mobj
def linkMsg(self, msg, isGroupChat=False): self.logger.info("---\nNew Link msg, %s", msg) # initiate object mobj = EFBMsg(self) # parse XML itchat.utils.emoji_formatter(msg, 'Content') xmldata = msg['Content'] data = xmltodict.parse(xmldata) # set attributes mobj.attributes = { "title": data['msg']['appmsg']['title'], "description": data['msg']['appmsg']['des'], "image": data['msg']['appmsg']['thumburl'], "url": data['msg']['appmsg']['url'] } if mobj.attributes['url'] is None: txt = mobj.attributes['title'] or '' txt += mobj.attributes['description'] or '' msg['Text'] = txt return self.textMsg(msg, isGroupChat) # format text mobj.text = "" if self._flag("extra_links_on_message", False): extra_link = data.get('msg', {}).get('appmsg', {}).get('mmreader', {}).get('category', {}).get('item', []) if type(extra_link) is list and len(extra_link): for i in extra_link: mobj.text += "🔗 %s\n%s\n%s\n🖼 %s\n\n" % ( i['title'], i['digest'], i['url'], i['cover']) mobj.type = MsgType.Link return mobj
def wechat_card_msg(self, msg): mobj = EFBMsg(self) txt = ("Name card: {NickName}\n" "From: {Province}, {City}\n" "QQ: {QQNum}\n" "ID: {Alias}\n" "Signature: {Signature}\n" "Gender: {Sex}") txt = txt.format(**msg['Text']) mobj.text = txt mobj.type = MsgType.Command mobj.attributes = { "commands": [ { "name": "Send friend request", "callable": "add_friend", "args": [], "kwargs": { "userName": msg['Text']['UserName'], "status": 2, "ticket": "" } } ] } return mobj
def wechat_voice_msg(self, msg): mobj = EFBMsg(self) mobj.type = MsgType.Audio mobj.path, mobj.mime = self.save_file(msg, mobj.type) mobj.text = None mobj.file = open(mobj.path, "rb") return mobj
def wechat_file_msg(self, msg): mobj = EFBMsg(self) mobj.type = MsgType.File mobj.path, mobj.mime = self.save_file(msg, mobj.type) mobj.text = msg['FileName'] mobj.filename = msg['FileName'] or None mobj.file = open(mobj.path, "rb") return mobj
def wechat_picture_msg(self, msg): mobj = EFBMsg(self) mobj.type = MsgType.Image if msg['MsgType'] == 3 else MsgType.Sticker mobj.path, mime = self.save_file(msg, mobj.type) mobj.text = None mobj.file = open(mobj.path, "rb") mobj.mime = mime return mobj
def pictureMsg(self, msg, isGroupChat=False): mobj = EFBMsg(self) mobj.type = MsgType.Image mobj.path, mime = self.save_file(msg, mobj.type) mobj.text = None mobj.file = open(mobj.path, "rb") mobj.mime = mime return mobj
def textMsg(self, msg, isGroupChat=False): self.logger.info("TextMsg!!!\n---") if msg['Text'].startswith( "http://weixin.qq.com/cgi-bin/redirectforward?args="): return self.locationMsg(msg, isGroupChat) mobj = EFBMsg(self) mobj.text = msg['Text'] mobj.type = MsgType.Text return mobj
def wechat_text_msg(self, msg): if msg['FromUserName'] == "newsapp" and msg['Content'].startswith("<mmreader>"): self.wechat_newsapp_msg(msg) return if msg['Text'].startswith("http://weixin.qq.com/cgi-bin/redirectforward?args="): self.wechat_location_msg(msg) return mobj = EFBMsg(self) mobj.text = msg['Text'] mobj.type = MsgType.Text return mobj
def exit_callback(self): msg = EFBMsg(self) msg.type = MsgType.Text msg.source = MsgSource.System msg.origin = { 'name': 'WeChat System Message', 'alias': 'WeChat System Message', 'uid': -1 } msg.text = "WeChat system logged out the user." self.queue.put(msg)
def linkMsg(self, msg, isGroupChat=False): self.logger.info("---\nNew Link msg, %s", msg) # initiate object mobj = EFBMsg(self) # parse XML itchat.utils.emoji_formatter(msg, 'Content') xmldata = msg['Content'] data = xmltodict.parse(xmldata) # set attributes mobj.attributes = { "title": data['msg']['appmsg']['title'], "description": data['msg']['appmsg']['des'], "image": None, "url": data['msg']['appmsg']['url'] } # format text mobj.text = "🔗 %s\n%s\n\n%s" % (mobj.attributes['title'], mobj.attributes['description'], mobj.attributes['url']) mobj.type = MsgType.Link return mobj
def friendMsg(self, msg, isGroupChat=False): mobj = EFBMsg(self) txt = ("Friend request: {NickName}\n" "From: {Province}, {City}\n" "QQ: {QQNum}\n" "ID: {Alias}\n" "Signature: {Signature}\n" "Gender: {Sex}") txt = txt.format(**{**msg['Text'], **msg['Text']['userInfo']}) mobj.text = txt mobj.type = MsgType.Command mobj.attributes = { "commands": [{ "name": "Send friend request", "callable": "add_friend", "args": [], "kwargs": { "userName": msg['Text']['userInfo']['UserName'], "status": 3, "ticket": msg['Ticket'] } }] } return mobj
def wechat_system_msg(self, msg): mobj = EFBMsg(self) mobj.text = "System message: %s" % msg['Text'] mobj.type = MsgType.Text return mobj
def systemMsg(self, msg, isGroupChat=False): mobj = EFBMsg(self) mobj.text = "System message: %s" % msg['Text'] mobj.type = MsgType.Text return mobj