一、下载Python3.6.5
地址:https://www.python.org/downloads/windows/
似乎那个站点网络比较慢,下载时需要耐心等待!
二、安装Python3.6.5
三、测试环境变量配置
win+R 运行cmd
在黑框中输入: python
结果如下则安装环境配置正确,输入:exit(),可退出
继续输入:pip
结果如下则pip的环境配置正常
打印一句话试试:
继续输入:print("hello Guanxing")
到此为止python环境配置完毕!
四、WeChat聊天机器人
1.安装以管理员身份运行Cmd
2.输入pip install requsts 回车
3.输入pip install itchat
ps:如电脑上有多个版本的,用pip加版本号进行安装(如pip2.7)
4.编写程序
demo1:自动进行回复消息机器人
# -*- coding=utf-8 -*- import requests import itchat import random KEY = '6244875a21b14000b1f350f9145e41f1' def get_response(msg): print(msg) apiUrl = 'http://www.tuling123.com/openapi/api' data = { 'key' : KEY, 'info' : msg, 'userid' : 'wechat-robot', } try: r = requests.post(apiUrl, data=data).json() # print(r) # print(r.get('text')) return r.get('text') except: return @itchat.msg_register(itchat.content.TEXT) def tuling_reply(msg): defaultReply = 'I received: ' + msg['Text'] robots=['——Gx'] reply = get_response(msg['Text'])+random.choice(robots) return reply or defaultReply itchat.auto_login(enableCmdQR=True) itchat.run()
demo2:指定时间给指定人发送消息
from __future__ import unicode_literals from threading import Timer from wxpy import * import requests bot = Bot() def get_news1(): # 获取金山词霸每日一句,英文和翻译 url = "http://open.iciba.com/dsapi/" r = requests.get(url) contents = r.json()['content'] translation = r.json()['translation'] print(contents) return contents, translation def send_news(): try: my_friend = bot.friends().search(u'Summer°')[0] # 你朋友的微信名称,不是备注,也不是微信帐号。 my_friend.send(get_news1()[0]) my_friend.send(get_news1()[1][5:]) my_friend.send(u"--来自爸爸的心灵鸡汤!") #每86400秒(1天),发送1次,不用linux的定时任务是因为每次登陆都需要扫描二维码登陆,很麻烦的一件事,就让他一直挂着吧 t = Timer(86400, send_news) t.start() except: my_friend = bot.friends().search('管星')[0] # # 你的微信名称,不是微信帐号。 my_friend.send(u"今天消息发送失败了") # # if __name__ == "__main__": send_news()
将代码复制进记事本中,然后后缀改成.py即可!
后面有时间再补充VS Code中编辑器环境的配置