mirror of
https://github.com/littlecodersh/ItChat.git
synced 2026-10-27 04:20:09 +08:00
Fix FileName key of attachment download & auto-add of new friend [RB#43: pcloth]
This commit is contained in:
@@ -4,8 +4,6 @@
|
||||
|
||||
itchat是一个开源的微信个人号接口,使用他你可以轻松的通过命令行使用个人微信号。
|
||||
|
||||
微信机器人的实现及命令行版微信见[robot分支](https://github.com/littlecodersh/ItChat/tree/robot)。
|
||||
|
||||
使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人。
|
||||
|
||||
如今微信已经成为了个人社交的很大一部分,希望这个项目能够帮助你扩展你的个人的微信号、方便自己的生活。
|
||||
@@ -36,15 +34,13 @@ def text_reply(msg):
|
||||
|
||||
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
|
||||
def download_files(msg):
|
||||
fileDir = '%s%s'%(msg['Type'], int(time.time()))
|
||||
msg['Text'](fileDir)
|
||||
itchat.send('%s received'%msg['Type'], msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', fileDir), msg['FromUserName'])
|
||||
msg['Text'](msg['FileName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
|
||||
return '%s received'%msg['Type']
|
||||
|
||||
@itchat.msg_register('Friends')
|
||||
def add_friend(msg):
|
||||
itchat.add_friend(**msg['Text'])
|
||||
itchat.get_contract()
|
||||
itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录
|
||||
itchat.send_msg('Nice to meet you!', msg['RecommendInfo']['UserName'])
|
||||
|
||||
@itchat.msg_register('Text', isGroupChat = True)
|
||||
@@ -85,14 +81,16 @@ itchat.auto_login(hotReload = True)
|
||||
|
||||
itchat的附件下载方法存储在msg的Text键中。
|
||||
|
||||
发送的文件的文件名(图片给出的默认文件名)都存储在msg的FileName键中。
|
||||
|
||||
下载方法接受一个可用的位置参数(包括文件名),并将文件相应的存储。
|
||||
|
||||
```python
|
||||
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
|
||||
def download_files(msg):
|
||||
msg['Text'](msg['FileName'])
|
||||
itchat.send('%s received'%msg['Type'], msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', fileDir), msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
|
||||
return '%s received'%msg['Type']
|
||||
```
|
||||
|
||||
## Have a try
|
||||
|
||||
+9
-9
@@ -37,16 +37,14 @@ Here is the `code <https://github.com/littlecodersh/ItChat/tree/robot>`__.
|
||||
|
||||
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
|
||||
def download_files(msg):
|
||||
fileDir = '%s%s'%(msg['Type'], int(time.time()))
|
||||
msg['Text'](fileDir)
|
||||
itchat.send('%s received'%msg['Type'], msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', fileDir), msg['FromUserName'])
|
||||
msg['Text'](msg['FileName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
|
||||
return '%s received'%msg['Type']
|
||||
|
||||
@itchat.msg_register('Friends')
|
||||
def add_friend(msg):
|
||||
itchat.add_friend(**msg['Text'])
|
||||
itchat.get_contract()
|
||||
itchat.send('Nice to meet you!', msg['RecommendInfo']['UserName'])
|
||||
itchat.add_friend(**msg['Text']) # new friend will be automatically added into storage, you don't need to reload the memberList
|
||||
itchat.send_msg('Nice to meet you!', msg['RecommendInfo']['UserName'])
|
||||
|
||||
@itchat.msg_register('Text', isGroupChat = True)
|
||||
def text_reply(msg):
|
||||
@@ -85,6 +83,8 @@ By using the following command, you may reload the program without re-scan QRCod
|
||||
|
||||
The attachment download function of itchat is in Text key of msg
|
||||
|
||||
Name of the file (default name of picture) is in FileName key of msg
|
||||
|
||||
Download function accept one location value (include the file name) and store attachment accordingly.
|
||||
|
||||
.. code:: python
|
||||
@@ -92,8 +92,8 @@ Download function accept one location value (include the file name) and store at
|
||||
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
|
||||
def download_files(msg):
|
||||
msg['Text'](msg['FileName'])
|
||||
itchat.send('%s received'%msg['Type'], msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', fileDir), msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
|
||||
return '%s received'%msg['Type']
|
||||
|
||||
**FAQ**
|
||||
|
||||
|
||||
+8
-10
@@ -4,8 +4,6 @@
|
||||
|
||||
itchat is an open source api for WeChat, a commonly-used Chinese social networking app, you can easily access your personal wechat account through itchat in cmd.
|
||||
|
||||
Wechat is structured with a wechat robot and a command line, you can view them in [robot branch](https://github.com/littlecodersh/ItChat/tree/robot).
|
||||
|
||||
A wechat robot can handle all the basic messages with only less than 30 lines of codes.
|
||||
|
||||
Now Wechat is an important part of personal life, hopefully this repo can help you extend your personal wechat account's functionality and enbetter user's experience with wechat.
|
||||
@@ -35,15 +33,13 @@ def text_reply(msg):
|
||||
|
||||
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
|
||||
def download_files(msg):
|
||||
fileDir = '%s%s'%(msg['Type'], int(time.time()))
|
||||
msg['Text'](fileDir)
|
||||
itchat.send('%s received'%msg['Type'], msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', fileDir), msg['FromUserName'])
|
||||
msg['Text'](msg['FileName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
|
||||
return '%s received'%msg['Type']
|
||||
|
||||
@itchat.msg_register('Friends')
|
||||
def add_friend(msg):
|
||||
itchat.add_friend(**msg['Text'])
|
||||
itchat.get_contract()
|
||||
itchat.add_friend(**msg['Text']) # new friend will be automatically added into storage, you don't need to reload the memberList
|
||||
itchat.send_msg('Nice to meet you!', msg['RecommendInfo']['UserName'])
|
||||
|
||||
@itchat.msg_register('Text', isGroupChat = True)
|
||||
@@ -84,14 +80,16 @@ itchat.auto_login(hotReload = True)
|
||||
|
||||
The attachment download function of itchat is in Text key of msg
|
||||
|
||||
Name of the file (default name of picture) is in FileName key of msg
|
||||
|
||||
Download function accept one location value (include the file name) and store attachment accordingly.
|
||||
|
||||
```python
|
||||
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
|
||||
def download_files(msg):
|
||||
msg['Text'](msg['FileName'])
|
||||
itchat.send('%s received'%msg['Type'], msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', fileDir), msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
|
||||
return '%s received'%msg['Type']
|
||||
```
|
||||
|
||||
## Have a try
|
||||
|
||||
+8
-10
@@ -4,8 +4,6 @@
|
||||
|
||||
itchat是一个开源的微信个人号接口,使用他你可以轻松的通过命令行使用个人微信号。
|
||||
|
||||
微信机器人的实现及命令行版微信见[robot分支](https://github.com/littlecodersh/ItChat/tree/robot)。
|
||||
|
||||
使用不到三十行的代码,你就可以完成一个能够处理所有信息的微信机器人。
|
||||
|
||||
如今微信已经成为了个人社交的很大一部分,希望这个项目能够帮助你扩展你的个人的微信号、方便自己的生活。
|
||||
@@ -36,15 +34,13 @@ def text_reply(msg):
|
||||
|
||||
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
|
||||
def download_files(msg):
|
||||
fileDir = '%s%s'%(msg['Type'], int(time.time()))
|
||||
msg['Text'](fileDir)
|
||||
itchat.send('%s received'%msg['Type'], msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', fileDir), msg['FromUserName'])
|
||||
msg['Text'](msg['FileName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
|
||||
return '%s received'%msg['Type']
|
||||
|
||||
@itchat.msg_register('Friends')
|
||||
def add_friend(msg):
|
||||
itchat.add_friend(**msg['Text'])
|
||||
itchat.get_contract()
|
||||
itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录
|
||||
itchat.send_msg('Nice to meet you!', msg['RecommendInfo']['UserName'])
|
||||
|
||||
@itchat.msg_register('Text', isGroupChat = True)
|
||||
@@ -85,14 +81,16 @@ itchat.auto_login(hotReload = True)
|
||||
|
||||
itchat的附件下载方法存储在msg的Text键中。
|
||||
|
||||
发送的文件的文件名(图片给出的默认文件名)都存储在msg的FileName键中。
|
||||
|
||||
下载方法接受一个可用的位置参数(包括文件名),并将文件相应的存储。
|
||||
|
||||
```python
|
||||
@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
|
||||
def download_files(msg):
|
||||
msg['Text'](msg['FileName'])
|
||||
itchat.send('%s received'%msg['Type'], msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', fileDir), msg['FromUserName'])
|
||||
itchat.send('@%s@%s'%('img' if msg['Type'] == 'Picture' else 'fil', msg['FileName']), msg['FromUserName'])
|
||||
return '%s received'%msg['Type']
|
||||
```
|
||||
|
||||
## Have a try
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ def load_login_status(fileDir = 'itchat.pkl'): return __client.load_login_status
|
||||
def send_msg(msg = 'Test Message', toUserName = None): return __client.send_msg(msg, toUserName)
|
||||
def send_file(fileDir, toUserName): return __client.send_file(fileDir, toUserName)
|
||||
def send_image(fileDir, toUserName): return __client.send_image(fileDir, toUserName)
|
||||
def add_friend(Status, UserName, Ticket): return __client.add_friend(Status, UserName, Ticket)
|
||||
def add_friend(status, userName, ticket, recommendInfo = {}): return __client.add_friend(status, userName, ticket, recommendInfo)
|
||||
def create_chatroom(memberList, topic = ''): return __client.create_chatroom(memberList, topic)
|
||||
def delete_member_from_chatroom(chatRoomUserName, memberList): return __client.delete_member_from_chatroom(chatRoomUserName, memberList)
|
||||
def add_member_into_chatroom(chatRoomUserName, memberList): return __client.add_member_into_chatroom(chatRoomUserName, memberList)
|
||||
|
||||
+19
-7
@@ -279,8 +279,10 @@ class client(object):
|
||||
for block in r.iter_content(1024):
|
||||
f.write(block)
|
||||
msg = {
|
||||
'Type': 'Picture',
|
||||
'Text': download_picture,}
|
||||
'Type' : 'Picture',
|
||||
'FileName' : '%s.%s'%(time.strftime('%y%m%d-%H%M%S', time.localtime()),
|
||||
'png' if m['MsgType'] == 3 else 'gif'),
|
||||
'Text' : download_picture, }
|
||||
elif m['MsgType'] == 34: # voice
|
||||
def download_voice(voiceDir):
|
||||
url = '%s/webwxgetvoice'%self.loginInfo['url']
|
||||
@@ -298,9 +300,10 @@ class client(object):
|
||||
msg = {
|
||||
'Type': 'Friends',
|
||||
'Text': {
|
||||
'Status': m['Status'],
|
||||
'UserName': m['RecommendInfo']['UserName'],
|
||||
'Ticket': m['Ticket'], }, }
|
||||
'status' : m['Status'],
|
||||
'userName' : m['RecommendInfo']['UserName'],
|
||||
'ticket' : m['Ticket'],
|
||||
'recommendInfo' : m['RecommendInfo'], }, }
|
||||
elif m['MsgType'] == 42: # name card
|
||||
msg = {
|
||||
'Type': 'Card',
|
||||
@@ -323,7 +326,6 @@ class client(object):
|
||||
f.write(block)
|
||||
msg = {
|
||||
'Type': 'Attachment',
|
||||
# 'FileName': m['FileName'],
|
||||
'Text': download_atta, }
|
||||
elif m['AppMsgType'] == 17:
|
||||
msg = {
|
||||
@@ -482,7 +484,7 @@ class client(object):
|
||||
'Content-Type': 'application/json;charset=UTF-8', }
|
||||
r = self.s.post(url, data = json.dumps(payloads, ensure_ascii = False).encode('utf8'), headers = headers)
|
||||
return True
|
||||
def add_friend(self, status, userName, ticket):
|
||||
def add_friend(self, status, userName, ticket, recommendInfo = {}):
|
||||
url = '%s/webwxverifyuser?r=%s&pass_ticket=%s'%(self.loginInfo['url'], int(time.time()), self.loginInfo['pass_ticket'])
|
||||
payloads = {
|
||||
'BaseRequest': self.loginInfo['BaseRequest'],
|
||||
@@ -497,6 +499,16 @@ class client(object):
|
||||
'skey': self.loginInfo['skey'], }
|
||||
headers = { 'ContentType': 'application/json; charset=UTF-8' }
|
||||
r = self.s.post(url, data = json.dumps(payloads), headers = headers)
|
||||
if recommendInfo: # add user to storage
|
||||
member = {}
|
||||
for k in ('UserName', 'City', 'DisplayName', 'PYQuanPin', 'RemarkPYInitial', 'Province',
|
||||
'KeyWord', 'RemarkName', 'PYInitial', 'EncryChatRoomId', 'Alias', 'Signature',
|
||||
'NickName', 'RemarkPYQuanPin', 'HeadImgUrl'): member[k] = ''
|
||||
for k in ('UniFriend', 'Sex', 'AppAccountFlag', 'VerifyFlag', 'ChatRoomId', 'HideInputBarFlag',
|
||||
'AttrStatus', 'SnsFlag', 'MemberCount', 'OwnerUin', 'ContactFlag', 'Uin',
|
||||
'StarFriend', 'Statues'): member[k] = 0
|
||||
member['MemberList'] = []
|
||||
self.memberList.append(dict(member, **recommendInfo))
|
||||
def create_chatroom(self, memberList, topic = ''):
|
||||
url = ('%s/webwxcreatechatroom?pass_ticket=%s&r=%s'%(
|
||||
self.loginInfo['url'], self.loginInfo['pass_ticket'], int(time.time())))
|
||||
|
||||
Reference in New Issue
Block a user