zoukankan      html  css  js  c++  java
  • Python3 itchat实现微信定时发送群消息

    Python3 itchat实现微信定时发送群消息

    一、简介

    1,使用微信,定时往指定的微信群里发送指定信息。

    2,需要发送的内容使用excel进行维护,指定要发送的微信群名、时间、内容。

    二、py库

    1,itchat:这个是主要的工具,用于连接微信个人账号接口。以下是一些相关的知识点网站。

    2,xlrd:这个是用来读Excel文件的工具。

    3,apscheduler:这个是用来定时调度时间的工具。

    三、实例代码

    # coding=utf-8
    from datetime import datetime
    import itchat
    import xlrd
    from apscheduler.schedulers.background import BlockingScheduler
    import os
    
    def SentChatRoomsMsg(name, context):
        itchat.get_chatrooms(update=True)
        iRoom = itchat.search_chatrooms(name)
        for room in iRoom:
            if room['NickName'] == name:
                userName = room['UserName']
                break
        itchat.send_msg(context, userName)
        print("发送时间:" + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "
    "
            "发送到:" + name + "
    "
            "发送内容:" + context + "
    ")
        print("*********************************************************************************")
        scheduler.print_jobs()
    
    def loginCallback():
        print("***登录成功***")
    
    def exitCallback():
        print("***已退出***")
    
    itchat.auto_login(hotReload=True, enableCmdQR=True, loginCallback=loginCallback, exitCallback=exitCallback)
    workbook = xlrd.open_workbook(
        os.path.join(os.path.dirname(os.path.realpath(__file__)), "chatroomsfileAutoSentChatroom.xlsx"))
    # workbook = xlrd.open_workbook("D:PyCharmCodeAutoLiulishouWechatchatroomsfileAutoSentChatroom.xlsx")
    sheet = workbook.sheet_by_name('Chatrooms')
    iRows = sheet.nrows
    
    scheduler = BlockingScheduler()
    index = 1
    for i in range(1, iRows):
        textList = sheet.row_values(i)
        name = textList[0]
        context = textList[2]
        float_dateTime = textList[1]
        date_value = xlrd.xldate_as_tuple(float_dateTime, workbook.datemode)
        date_value = datetime(*date_value[:5])
        if datetime.now() > date_value:
            continue
        date_value = date_value.strftime('%Y-%m-%d %H:%M:%S')
        textList[1] = date_value
        scheduler.add_job(SentChatRoomsMsg, 'date', run_date=date_value,
          kwargs={"name": name, "context": context})
        print("任务" + str(index) + ":
    "
          "待发送时间:" + date_value + "
    "
          "待发送到:" + name + "
    "
          "待发送内容:" + context + "
    "
          "******************************************************************************
    ")
        index = index + 1
    
    if index == 1:
        print("***没有任务需要执行***")
    scheduler.start()
  • 相关阅读:
    51 nod 1181 质数中的质数(质数筛法)
    Just oj 2018 C语言程序设计竞赛(高级组)F:Star(结构体排序+最小生成树)
    欧拉函数+费马小定理拓展
    ZOJ 3785 What day is that day?(数论:费马小定理)
    Just oj 2018 C语言程序设计竞赛(高级组)H: CBT?
    树链剖分(入门学习)
    bitset用法
    链式前向星
    Nearest Common Ancestors(LCA板子)
    LCA(最近公共祖先)
  • 原文地址:https://www.cnblogs.com/lizm166/p/9670033.html
Copyright © 2011-2022 走看看