微信定时自动发送群消息的小工具-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文件中的timeroomNamecontext设置为自己需要的参数。具体参考如下:

    1
    2
    3
    roomName = u'两张王'
    context=u'该消息由程序自动发送'
    time = {'day_of_week':'*','hour':12,'minute':34,'second':26}
  • 运行python onTimeMsgSenderToChatroom.py;

  • 扫描QR码,自动登陆成功后,等待信息自动发送即可。

示例

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

msgOnTimeSenderTest

源代码

需要下载请到我的github

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- 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)

文章目录
  1. 1. 设计目的
  2. 2. 依赖条件
  3. 3. 功能描述
  4. 4. 使用方法
  5. 5. 示例
  6. 6. 源代码
|