代码如下:
"""
Created on Wed Mar 14 14:37:09 2018
@author: zhanghong
"""
import datetime
import itchat
import os
import requests
import time
from threading import Timer
#热登陆
itchat.auto_login(hotReload=True)
users1 = itchat.search_friends(name=u'丽莎')
username1 = users1[0]['UserName']
users2 = itchat.search_friends(name=u'胡灿')
username2 = users2[0]['UserName']
#调用图灵机器人
def get_response(msg):
apiUrl = 'http://www.tuling123.com/openapi/api'
data = {
'key': key,
'info': msg,
'userid': 'pth-robot',
}
try:
r = requests.post(apiUrl, data=data).json()
return r.get('text')
except:
return
def tuling_reply(addr):
weather_reply = get_response(addr)
return weather_reply
def send_msg():
weather_reply_cs = tuling_reply('长沙天气')
weather_reply_yy = tuling_reply('益阳天气')
delta = today - d1
i = delta.days + 1
j = delta.days + 1001
#判断时间
if time.strftime('%H') == '7':
itchat.send('丽莎,早上好!\n' + weather_reply_cs,toUserName=username1)
itchat.send('亲爱的灿灿,早上好!\n' + weather_reply_yy,toUserName=username2)
itchat.send("@img@%s" % str(i) + '.jpg',toUserName=username2)
print('发送成功')
else:
if time.strftime('%H') == '23':
itchat.send('亲爱的,晚安!^.^\n',toUserName=username2)
itchat.send("@img@%s" % str(j) + '.jpg',toUserName=username2)
print('发送成功')
global timer
timer = Timer(3600, send_msg)
timer.start()
#开启定时器
timer = Timer(3600, send_msg)
timer.start()
#定时器取消
#time.sleep(10)
#timer.cancel()
领取专属 10元无门槛券
私享最新 技术干货