Timer(5, send_news)
每日一句,发送至多人,多个群
1 # -*- coding: utf-8 -*- 2 #from __future__ import unicode_literals 3 from threading import Timer 4 from wxpy import * 5 import requests 6 bot = None 7 def get_news1(): 8 #获取金山词霸每日一句,英文和翻译 9 url = "http://open.iciba.com/dsapi/" 10 r = requests.get(url) 11 print(r.json()) 12 contents = r.json()[‘content‘] 13 note = r.json()[‘note‘] 14 translation = r.json()[‘translation‘] 15 return contents,note,translation 16 def login_wechat(): 17 18 global bot 19 #bot = Bot() 20 bot = Bot(console_qr=2,cache_path="botoo.pkl")#Linux专用,像素二维码 21 22 def send_news(): 23 if bot == None: 24 login_wechat() 25 try: 26 my0_friend = bot.friends().search(u‘王琳杰‘)[0] #你朋友的微信名称,不是备注,也不是微信帐号。 27 my1_friend = bot.friends().search(u‘浮生若梦‘)[0] 28 #my2_friend = bot.friends().search(u‘e.g.‘)[0] 29 30 my0_groups = bot.groups().search(u‘聊天机器人测试‘)[0] #你群的微信名称。 31 my1_groups = bot.groups().search(u‘测试‘)[0] 32 33 my0_friend.send(get_news1()[0]) 34 #my_friend.send(get_news1()[1]) 35 #my_friend.send(get_news1()[2]) 36 #my_friend.send(get_news1()[1][5:]) 37 my1_friend.send(get_news1()[0]) 38 #my2_friend.send(get_news1()[0]) 39 40 my0_groups.send(get_news1()[0]) 41 my1_groups.send(get_news1()[0]) 42 #t = Timer(86400, send_news) #每86400秒(1天),发送1次,不用linux的定时任务是因为每次登陆都需要扫描二维码登陆,很麻烦的一件事,就让他一直挂着吧 43 t = Timer(5, send_news) 44 t.start() 45 except: 46 print(u"今天消息发送失败了") 47 48 ‘‘‘ 49 schedule.every().day.at("12:30").do(send_news) #规定每天12:30执行job()函数 50 while True: 51 schedule.run_pending()#确保schedule一直运行 52 time.sleep(1) 53 ‘‘‘ 54 55 if __name__ == "__main__": 56 send_news() 57 #print(get_news1()[0]) 58 #print(get_news1()[1][5:])