微信定时自动发送群消息的小工具-python-itchat
生活随笔
收集整理的這篇文章主要介紹了
微信定时自动发送群消息的小工具-python-itchat
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 摘要
- 設計目的
- 依賴條件
- 功能描述
- 使用方法
- 示例
- 源代碼
- 參考文獻
摘要
本程序實現定時向指定微信群發送指定消息的功能。
主要工具如下:
- python2.7編寫;
- itchat提供微信API接口,實現進行向群組發送消息;
- apscheduler提供定時器功能。
代碼下載請到我的github倉庫。
設計目的
最開始只是想能不能做一個程序,可以定時在周二,四,六晚上10點半提醒組員們上傳日志,所以就開始了搜索。發現itchat這個超級好用的工具,在apscheduler的加持下最終編了這么一個程序。靈感總是來源于生活….
當然itchat的功能遠不止是發送個群消息而已,它提供的微信API接口還可以做很多有意思的東西,比如最常見的機器人自動回復,還可以接入其他應用程序接口用微信號做一些小應用(如自動播放歌曲,返回天氣信息等)。
依賴條件
Written in python2.7.12:
- itchat 1.3.10
- APScheduler 3.5.1
功能描述
定義文件中的發送時間time,發送群組名roomName,發送消息context(消息中含中文字符時應該使用unicode編碼)。
運行后會在time對應時間,往roomName對應群組,發送context對應的消息。
使用方法
- 修改onTimeMsgSenderToChatroom.py文件中的time,roomName,context設置為自己需要的參數。具體參考如下:
- 運行python onTimeMsgSenderToChatroom.py;
- 掃描QR碼,自動登陸成功后,等待信息自動發送即可。
示例
以下是用按照上述設置運行源代碼向微信群發送的消息的截圖。
源代碼
需要下載請到我的github。
# -*- coding:utf-8 -*-import itchat from datetime import datetime from apscheduler.schedulers.blocking import BlockingSchedulerclass onTimeSender(object):'''Automatically send message to chatroom on time according to user's predefinition.params:`roomName`: The name of chatroom you want to send message to;`context`: The message you want to send to chatroom;`time`: Time to send out message, in form of a dict.Usage:Please refer to the __main__ function part.'''def __init__(self,roomName='',context='',time={}):self.roomName=roomNameself.context=contextself.time=time itchat.auto_login(hotReload=True,loginCallback=self.loginCallback,exitCallback=self.exitCallback)self.schedulerForSender()def sendChatroomMsg(self,roomName,context):itchat.get_chatrooms(update=True)roomNickName = roomNamecandidates = itchat.search_chatrooms(roomNickName)print candidatesusername = ''for candidate in candidates:if candidate['NickName'] == roomNickName:username = candidate['UserName']breakif username:sendtime = datetime.now().strftime('%A %B %d,%Y')#Tue June 08,2018sendtime = datetime.now().strftime('%m-%d-%Y %H:%M:%S,%A')msg = context + "Sending in "+sendtimeprint "Ready to send message to group %s,message as follows : \n%s"%(roomName,msg)itchat.send_msg(msg=msg,toUserName=username)def loginCallback(self):print "Successfully logged in."def exitCallback(self):print "Successfully logged out."def sendMsgToChatRoom(self):self.sendChatroomMsg(self.roomName,self.context)def schedulerForSender(self):# scheduler setupscheduler = BlockingScheduler()scheduler.add_job(self.sendMsgToChatRoom,'cron',day_of_week=self.time['day_of_week'],hour=self.time['hour'],minute=self.time['minute'],second=self.time['second'])# sending takes 4 seconds behindscheduler.start()if __name__=='__main__':roomName = 'DVS_Group'context = u'通知上傳日志'time = {'day_of_week':'tue,thu,sat','hour':22,'minute':24,'second':56}'''for testingroomName = u'兩張王'context=u'該消息由程序自動發送'time = {'day_of_week':'*','hour':12,'minute':34,'second':26}'''onTimeSender(roomName,context,time)參考文獻
apscheduler使用
itchat使用文檔
總結
以上是生活随笔為你收集整理的微信定时自动发送群消息的小工具-python-itchat的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对象创建过程之二(类加载器)
- 下一篇: ext2 源代码解析之 “从路径名到目标