1, file transfer assistant to send a message
itchat Import
itchat.auto_login (enableCmdQR = True) # here you need to manually scan code to log phone
itchat.send ( 'Hello, File Helpers', toUserName = 'filehelper')
2, reply to a text message sent to their own
itchat Import
@ itchat.msg_register (itchat.content.TEXT)
DEF text_replay (msg):
return msg.text # what other people say what your reply. . .
itchat.auto_login ()
itchat.run ()
3, mass assistant
itchat Import
Import Time
itchat.auto_login (hotReload = True, enableCmdQR = True)
"!% s wish Happy Thanksgiving" SINCERE_WISH = U
riend_list = itchat.get_friends (Update = True) # The first one is its own
friend_list = friend_list [1: ]
for Friend in FRIEND_LIST:
# If the demo, to send into print on the line
# itchat.send (SINCERE_WISH% (Friend [ 'the DisplayName'] or Friend [ 'the NickName']), Friend [ 'UserName'])
print (SINCERE_WISH % (Friend [ 'the DisplayName'] or Friend [ 'the NickName']))
the time.sleep (. 3)
4, delete detect Friends
"" "Sometimes we will want to know that a friend has not deleted themselves or pull themselves into the blacklist.
This operation uses itchat also become very simple.
When the principle, then, that the friends pulled into the group chat, non-friend blacklist friends and not be pulled into the group chat.
so the return value of a group chat in the data can buddy relationship with you.
in addition, group chat outside will be created in addition to the first time a message is generated by ordinary people (not a common message system messages) found in
this way, can detect concealed complete buddy
"" "
Import itchat
CHATROOM_NAME = 'friend'
CHATROOM = None
HELP_MSG = U '' '\
buddy status monitoring
* sending the card returns a friend state
* Make sure there is a group called% s unused talk
* to save the group and chat contacts
* call there is a certain limit frequency \
'' '% CHATROOM_NAME
CHATROOM_MSG = U' '' \
can not automatically create a group chat, manually created
to ensure that the group chat name is% s
Please do not use group chat has been used
to create a hangout place after save to address book \
'' '% CHATROOM_NAME
DEF get_chatroom ():
, Ltd. Free Join CHATROOM
IF CHATROOM iS None:
itchat.get_chatrooms(update=True)
chatrooms = itchat.search_chatrooms(CHATROOM_NAME)
if chatrooms:
return chatrooms[0]
else:
r = itchat.create_chatroom(itchat.get_friends()[1:4], topic=CHATROOM_NAME)
if r['BaseResponse']['ErrMsg'] == '':
CHATROOM = {'UserName': r['ChatRoomName']}
return CHATROOM
else:
return CHATROOM
def get_friend_status(friend):
ownAccount = itchat.get_friends(update=True)[0]
if friend['UserName'] == ownAccount['UserName']:
return u'检测到本人账号。'
elif itchat.search_friends(userName=friend['UserName']) is None:
return u'该用户不在你的好友列表中。'
else:
chatroom = CHATROOM or get_chatroom()
if chatroom is None: return CHATROOM_MSG
itchat.add_member_into_chatroom = R & lt (ChatRoom [ 'UserName'], [Friend])
IF R & lt [ 'BaseResponse'] [ 'ErrMsg'] == '':
Status = R & lt [ 'memberList'] [0] [ 'MemberStatus']
itchat.delete_member_from_chatroom (ChatRoom [ 'UserName'], [friend])
return {. 3: U 'that friend you've blacklist. ',
4: U' the friends you've deleted. ',} .Get (Status,
U' is still with the friends you are friends relationship. ')
The else:
return U' can not get buddy status, is expected to have reached the limit call interface. '
@ Itchat.msg_register (itchat.content.CARD)
DEF get_friend (MSG):
IF MSG [' ToUserName '] =!' Filehelper ': return
friendStatus = get_friend_status (MSG [' RecommendInfo '])
itchat.send (friendStatus,' filehelper ')
itchat.
5, micro-channel music
"" "
This is a control computer to play music through a micro-channel small project, mainly three functions: Enter the" Help "to display help enter" close "to close the music player * enter specific song, select the song into the
" " "
Import os
Import itchat
from NetEaseMusicApi Import interact_select_song
HELP_MSG =" "" \
Welcome to the micro-channel Netease cloud music
help: displays help
Close: Close song
song: according to guide the music
"" "
with Open ( 'stop.mp3', 'w ') AS F:
Pass
DEF close_music ():
os.startfile (' stop.mp3 ')
@ itchat.msg_register (itchat.content.TEXT)
DEF music_player (MSG):
IF MSG [' ToUserName ']! =' filehelper ' :
return
IF MSG [ 'the Text'] == U 'off':
close_music ()
itchat.send (U 'off Music', 'filehelper')
if msg['Text'] == u'帮助':
itchat.send(HELP_MSG, 'filehelper')
else:
itchat.send(interact_select_song(msg['Text']), 'filehelper')
itchat.auto_login(True)
itchat.send(HELP_MSG, 'filehelper')
itchat.run()
6, to achieve the micro-channel message acquisition
import itchat
"""
图片对应itchat.content.PICTURE
语音对应itchat.content.RECORDING
名片对应itchat.content.CARD
TEXT = 'Text'
MAP = 'Map'
CARD = 'Card'
NOTE = 'Note'
SHARING = 'Sharing'
PICTURE = 'Picture'
RECORDING = VOICE = 'Recording'
ATTACHMENT = 'Attachment'
VIDEO = 'Video'
FRIENDS = 'Friends'
SYSTEM = 'System'
"""
@itchat.msg_register(itchat.content.TEXT)
def print_content(msg):
print(msg['Text'])
itchat.auto_login()
itchat.run()
7, micro-channel message transmission to achieve
itchat Import
"" "The example will return a text message intact" ""
@ itchat.msg_register (itchat.content.TEXT)
DEF print_content (MSG):
return MSG [ 'the Text']
itchat.auto_login ()
itchat. run ()
8, the simplest Turing interact with robots
Requests Import
Import itchat
KEY = '8edce3ce905adbb965e6b35c3834d'
DEF get_response (msg):
# Here we just did in "3. To achieve the simplest Turing interact with robots," as in
# is constructed to be sent to the data server
apiUrl = 'http://www.tuling123.com/openapi/api'
Data = {
'Key': KEY,
'info': MSG,
'the userid': 'WeChat-Robot',
}
the try:
R & lt requests.post = (apiUrl , Data = Data) .json ()
# dictionary in the dictionary does not get method 'text' when the value is returned without None thrown
return r.get ( 'text')
# in order to prevent the server does not normally result in response to the program abnormal exit, captured here by try-except exception
# If the server fails to properly interact with (non-return json or not connected), it will enter the following return
the except:
# will be a return None
return
# this is our "1 the same registration methods to achieve micro-channel message access "has been used in the
@itchat.msg_register(itchat.content.TEXT)
tuling_reply DEF (MSG):
# in order to ensure a problem when Turing Key can still reply, there is provided a default reply
defaultReply = 'Received the I:' MSG + [ 'the Text']
# Key problems if Turing, then reply It will be None
Reply = get_response (msg [ 'Text'])
# a or b mean, if there is a content, then a return, otherwise b
# the content generally refers to a non-empty or non None, you can use `if a: print ( 'True ')` to test the
return or defaultReply Reply
# to make the experiment more convenient (to modify the program without repeatedly scan code), we use hot start
itchat.auto_login (hotReload = True)
itchat.run ( )