Need
想要在服务器上部署微信机器人,实现定时发送消息
定时--使用datetime与time模块
每小时刷新一次,检测接下来的一个小时有没有任务,如果有,则创建线程等待发送
发送--使用wxpy
献上我的代码
from wxpy import *
import time
import datetime
from threading import Thread
THE_GROUP='GroupName'
raw={
1:[20,21,0,'好'],#[hour,mins,day(optional),'msg']
2:[20,22,0,'1'],
3:[20,23,0,'1'],
}
#parse time
def parse(list_,now):
# make it today
year=now.year
month=now.month
day=now.day
h=list_[0]
m=list_[1]
# if list_[2]:
# day=list_[2]
return datetime.datetime(year,month,day,h,m,0)
#parse time Done!
#ItChat
# bot=Bot(cache_path=True)
#Linux
bot = Bot(console_qr=2,cache_path="wechat.pkl")
#make sure there is an active groupchat
print('warnning:#make sure there is an active groupchat')
print('OR ,you have added the groupchat into the Contacts!')
target=bot.groups().search(THE_GROUP)[0]
def send(msg,sec,i):
print('Thread starts ,sleep %ds' %sec)
time.sleep(sec)
target.send(messages)
print('No.%d mseeage sended !'%int(i))
return
if __name__=='__main__':
while True:
print('Refreshing...')
for i in raw:
#Refresh now time
now=datetime.datetime.now()
target=parse(raw[i],now)
delta=target-now
if delta.days>=0:
if delta.seconds<60*60:# triggered before 1 hour of eta
print('initialize thread %d'%i)
eta=delta.seconds
msg=raw[i][-1]
t=Thread(target=send,args=(msg,eta,i))
t.start()
else:
print('No.%d not fill'%i)
else:
print('No.%d not fill'%i)
print('sleep 1 hour......')
time.sleep(60*60)
啊!!!!!!!
万事俱备,东风不来。在服务器端登不上微信,在本地可以。我还在server上上传了pkl登录文件,但是因为ip和手机端地理位置的原因吧,我猜,被block了,一定是WeChat Group从中作梗。还是好好学习,多学点服务器端的知识吧。