Python 学习篇(二):给妹子定时发消息

代码如下:

"""

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()

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180315G0LC2100?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 yunjia_community@tencent.com 删除。

扫码关注云+社区

领取腾讯云代金券