Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

使用ItChat库发微信信息 #110

Open Qingquan-Li opened 5 years ago

Qingquan-Li commented 5 years ago

开发环境:

一、

https://github.com/littlecodersh/ItChat

$ pip install itchat

二、

#!/urs/bin/python
# -*- coding: UTF-8 -*-

import itchat

# hotReload==True用于保留登录状态。该参数生成一个静态文件itchat.pkl用于存储登录状态
itchat.auto_login(hotReload=True)

# 发送信息给个人
# test = 'ThisIsTest'
# itchat.send(test, toUserName = 'filehelper')

# 发送信息给群聊
message = u'这是python机器人测试,请忽略'
chat_room_name = u'机器人测试'  # 群聊名称
itchat.get_chatrooms(update=True)  # 更新同步微信群聊
chat_rooms = itchat.search_chatrooms(name = chat_room_name)
if chat_rooms is None:
    print(u'没有找到群聊:' + chat_room_name)
else:
    # print(message, chat_rooms[0]['UserName'])
    itchat.send_msg(message, chat_rooms[0]['UserName'])

三、

$ python3.5 itchat_test.py