微信定时自动发送群消息的小工具

您所在的位置:网站首页 微信自动群发到每个群 微信定时自动发送群消息的小工具

微信定时自动发送群消息的小工具

2024-07-09 23:32| 来源: 网络整理| 查看: 265

摘要设计目的依赖条件功能描述使用方法示例源代码参考文献

摘要

本程序实现定时向指定微信群发送指定消息的功能。 主要工具如下: - 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设置为自己需要的参数。具体参考如下: roomName = u'两张王' context=u'该消息由程序自动发送' time = {'day_of_week':'*','hour':12,'minute':34,'second':26} 运行python onTimeMsgSenderToChatroom.py;扫描QR码,自动登陆成功后,等待信息自动发送即可。 示例

以下是用按照上述设置运行源代码向微信群发送的消息的截图。

msgOnTimeSenderTest

源代码

需要下载请到我的github。

# -*- coding:utf-8 -*- import itchat from datetime import datetime from apscheduler.schedulers.blocking import BlockingScheduler class 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=roomName self.context=context self.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 = roomName candidates = itchat.search_chatrooms(roomNickName) print candidates username = '' for candidate in candidates: if candidate['NickName'] == roomNickName: username = candidate['UserName'] break if username: sendtime = datetime.now().strftime('%A %B %d,%Y')#Tue June 08,2018 sendtime = datetime.now().strftime('%m-%d-%Y %H:%M:%S,%A') msg = context + "Sending in "+sendtime print "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 setup scheduler = 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 behind scheduler.start() if __name__=='__main__': roomName = 'DVS_Group' context = u'通知上传日志' time = {'day_of_week':'tue,thu,sat','hour':22,'minute':24,'second':56} '''for testing roomName = u'两张王' context=u'该消息由程序自动发送' time = {'day_of_week':'*','hour':12,'minute':34,'second':26} ''' onTimeSender(roomName,context,time) 参考文献

apscheduler使用 itchat使用文档



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3