python third-party libraries itchat

  1. Installation itchat
    PIP install itchat

2. Package guide
import itchat

3. landing operation
itchat.login () # landed just plain does not have the cache function
itchat.auto_login (hotReload = True) # login status will be retained, in a short time without re-landing

  1. Micro-channel message to the Mobile Assistant
    itchat.send ( 'hello', toUserName = 'filehelper')

The micro-channel to send assistant file
# itchat.send_file ( '/ etc / passwd ', toUserName = 'filehelper')

2. Statistical proportion of men and women your friends

friends = itchat.get_friends()
#print(friends)
info = {}
for friend in friends[1:]:
if friend['Sex'] == 1:
info['male'] = info.get('male',0) + 1
elif friend['Sex'] == 2:
info['female'] = info.get('female',0) + 1
else:
info['other'] = info.get('other',0) + 1
print(info)

3.
Import OS
# execute shell commands in python
# 1 may be determined whether the command executed successfully
# successful implementation of the return value is 0
# return value is not 0 Unsuccessful
Print (the os.system ( 'LS'))
#res = the os.system ( 'hostnameeeee')
#Print (RES)

# 2 for holding results of the command
RES = os.popen, ( 'hostname'). Read ()
Print (RES)

4. The micro-channel chat robot Turing

import itchat
import requests

get_tuling_reponse DEF (the _info):
Print (the _info)
api_url = ' http://www.tuling123.com/openapi/api ' provided Turing registered URL
Data = {
'Key': '28a1d488a7fe47b5b637b750a6f3d66b', registration Turing Robot Key
'info': _ info,
'the userid': 'haha'
}
# send data to the specified URL, the URL to obtain data returned by the
RES = requests.post (api_url, data) .json ()
#Print (RES, type (RES ))
# returns the user to the content
Print (RES [ 'text'])
return (RES [ 'text'])

#get_tuling_reponse ( 'Tell me a joke')
#get_tuling_reponse ( 'not funny')

# FriendSend time monitoring of textual information, and to give a reply br /> @ itchat.msg_register (itchat.content.TEXT, isFriendChat = True)
DEF text_repky (MSG):

Get text messages sent by friends

#返回文本信息
content = msg['Content']
#将好友的消息发送给机器人去处理,处理的结果就是返回给好友的信息
returnContent = get_tuling_reponse(content)
return returnContent

itchat.auto_login()
itchat.run()

Guess you like

Origin blog.51cto.com/12893781/2408368