diff --git a/README.md b/README.md index 65c4076..ad87ff0 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ `i->[昵称或ID]:[图片路径]` 给好友发送图片 +`e->[昵称或ID]:[文件路径]` 给好友发送表情(jpg/gif) + `quit` 退出程序 ![7](screenshot/7.png) @@ -385,6 +387,17 @@ selector: ... } ``` + +#### 发送 Emoji 表情 + +| API | webwxsendmsgemotion | +| --- | ------------ | +| url | https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendemoticon?fun=sys&f=json&pass_ticket=xxx | +| method | POST | +| data | JSON | +| header | ContentType: application/json; charset=UTF-8 | +| params | {
     BaseRequest: { Uin: xxx, Sid: xxx, Skey: xxx, DeviceID: xxx },
     Msg: {
         Type: 47 `emoji消息`,
         EmojiFlag: 2,
         MediaId: `表情上传后的媒体ID`,
         FromUserName: `自己ID`,
         ToUserName: `好友ID`,
         LocalID: `与clientMsgId相同`,
         ClientMsgId: `时间戳左移4位随后补上4位随机数`
     }
} | +
### 图片接口 diff --git a/weixin.py b/weixin.py index a6a2148..1e1834b 100755 --- a/weixin.py +++ b/weixin.py @@ -485,6 +485,31 @@ class WebWeixin(object): dic = r.json() return dic['BaseResponse']['Ret'] == 0 + def webwxsendmsgemotion(self, user_id, media_id): + url = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendemoticon?fun=sys&f=json&pass_ticket=%s' % self.pass_ticket + clientMsgId = str(int(time.time() * 1000)) + \ + str(random.random())[:5].replace('.', '') + data_json = { + "BaseRequest": self.BaseRequest, + "Msg": { + "Type": 47, + "EmojiFlag": 2, + "MediaId": media_id, + "FromUserName": self.User['UserName'], + "ToUserName": user_id, + "LocalID": clientMsgId, + "ClientMsgId": clientMsgId + } + } + headers = {'content-type': 'application/json; charset=UTF-8'} + data = json.dumps(data_json, ensure_ascii=False).encode('utf8') + r = requests.post(url, data=data, headers=headers) + dic = r.json() + if self.DEBUG: + print json.dumps(dic, indent=4) + logging.debug(json.dumps(dic, indent=4)) + return dic['BaseResponse']['Ret'] == 0 + def _saveFile(self, filename, data, api=None): fn = filename if self.saveSubFolders[api]: @@ -840,6 +865,14 @@ class WebWeixin(object): user_id = self.getUSerID(name) response = self.webwxsendmsgimg(user_id, media_id) + def sendEmotion(self, name, file_name): + response = self.webwxuploadmedia(file_name) + media_id = "" + if response is not None: + media_id = response['MediaId'] + user_id = self.getUSerID(name) + response = self.webwxsendmsgemotion(user_id, media_id) + @catchKeyboardInterrupt def start(self): self._echo('[*] 微信网页版 ... 开动') @@ -910,6 +943,11 @@ class WebWeixin(object): [name, file_name] = text[3:].split(':') self.sendImg(name, file_name) logging.debug('发送图片') + elif text[:3] == 'e->': + print '发送表情' + [name, file_name] = text[3:].split(':') + self.sendEmotion(name, file_name) + logging.debug('发送表情') def _safe_open(self, path): if self.autoOpen: