2021-7-30-python实现企业微信(员工)内部机器人发送功能

系列文章目录


前言

python实现企业微信(员工)内部机器人发送功能。
缺少文件的上传,需要文件上传的请绕道

一、发送content

import requests, json
import datetime
import time

# 文本类型消息
def send_msg_txt():
    send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="

    headers = {"Content-Type": "text/plain"}
    send_data = {
        "msgtype": "text",  # 消息类型,此时固定为text
        "text": {
            "content": "123"  # 文本内容,最长不超过2048个字节,必须是utf8编码
        }
    }

    requests.post(url=send_url, headers=headers, json=send_data)

send_msg_txt()

二、支持发送markdown文本

from threading import Timer
import requests
import random
import time
import schedule

# 文本类型消息
def send_msg_txt():
    send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
    headers = {"Content-Type": "text/plain"}
    send_data = {
    "msgtype": "markdown",
    "markdown": {
        "content": "[BOWWOB的博客](https://blog.csdn.net/BOWWOB?spm=1001.2101.3001.5343)实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n>类型:<font color=\"comment\">用户反馈</font>>普通用户反馈:<font color=\"comment\">117例</font>>VIP用户反馈:<font color=\"comment\">15例</font>"
    }
}

    requests.post(url=send_url, headers=headers, json=send_data)

send_msg_txt()

三、发送图片

# -*- coding:utf-8 -*-
import requests
import base64
import hashlib


def wx_image(image):
    with open(image, 'rb') as file:  # 转换图片成base64格式
        data = file.read()
        encodestr = base64.b64encode(data)
        image_data = str(encodestr, 'utf-8')

    with open(image, 'rb') as file:  # 图片的MD5值
        md = hashlib.md5()
        md.update(file.read())
        image_md5 = md.hexdigest()

    url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="  # 填上机器人Webhook地址
    headers = {"Content-Type": "application/json"}
    data = {
        "msgtype": "image",
        "image": {
            "base64": image_data,
            "md5": image_md5
        }
    }

    result = requests.post(url, headers=headers, json=data)

    return result


wx_image('E:\\py_image1.png')  # 传入图片路径

四、发送图文(图文各为一个超链接)

import requests
import json


class WXWork_SMS:

    # 图文类型消息
    def send_msg_txt_img(self):
        headers = {"Content-Type": "text/plain"}
        send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
        send_data = {
            "msgtype": "news",  # 消息类型,此时固定为news
            "news": {
                "articles": [  # 图文消息,一个图文消息支持1到8条图文
                    {
                        "title": "中秋节礼品领取",  # 标题,不超过128个字节,超过会自动截断
                        "description": "今年中秋节公司有豪礼相送",  # 描述,不超过512个字节,超过会自动截断
                        "url": "www.baidu.com",  # 点击后跳转的链接。
                        "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                        # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                    },
                    {
                        "title": "我的CSDN - 魏风物语",  # 标题,不超过128个字节,超过会自动截断
                        "description": "坚持每天写一点点",  # 描述,不超过512个字节,超过会自动截断
                        "url": "https://blog.csdn.net/BOWWOB",  # 点击后跳转的链接。
                        "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                        # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                    }
                ]
            }
        }

        res = requests.post(url=send_url, headers=headers, json=send_data)
        print(res.text)


if __name__ == '__main__':
    sms = WXWork_SMS()
    # 图文类型消息
    sms.send_msg_txt_img()

五、周期性-发送content

#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create type_time: 2020-2-24
Info: 定期向企业微信推送消息
"""
import requests, json
import datetime
import time

wx_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="    # 测试机器人1号
send_message = "测试:测试机器人1号………………………………!"


def get_current_time():
    """获取当前时间,当前时分秒"""
    now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    hour = datetime.datetime.now().strftime("%H")
    mm = datetime.datetime.now().strftime("%M")
    ss = datetime.datetime.now().strftime("%S")
    return now_time, hour, mm, ss


def sleep_time(hour, m, sec):
    """返回总共秒数"""
    return hour * 3600 + m * 60 + sec


def send_msg(content):
    """艾特全部,并发送指定信息"""
    data = json.dumps({"msgtype": "text", "text": {"content": content, "mentioned_list":["@all"]}})
    r = requests.post(wx_url, data, auth=('Content-Type', 'application/json'))
    print(r.json)


def every_time_send_msg(interval_h=0, interval_m=0, interval_s=2, special_h="00", special_m="00", mode="special"):
    """每天指定时间发送指定消息"""

    # 设置自动执行间隔时间
    second = sleep_time(interval_h, interval_m, interval_s)
    # 死循环
    while 1 == 1:
        # 获取当前时间和当前时分秒
        c_now, c_h, c_m, c_s = get_current_time()
        print("当前时间:", c_now, c_h, c_m, c_s)
        if mode == "special":
            if c_h == special_h and c_m == special_m:
                # 执行
                print("正在发送...")
                send_msg(send_message)
        else:
            send_msg(send_message)
        print("每隔" + str(interval_h) + "小时" + str(interval_m) + "分" + str(interval_s) + "秒执行一次")
        # 延时
        time.sleep(second)


if __name__ == '__main__':
    every_time_send_msg(mode="no")

六、周期性-支持发送markdown文本

#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create type_time: 2020-2-24
Info: 定期向企业微信推送消息
"""
import requests, json
import datetime
import time

wx_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="    # 测试机器人1号
send_message = "测试:测试机器人1号………………………………!"


def get_current_time():
    """获取当前时间,当前时分秒"""
    now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    hour = datetime.datetime.now().strftime("%H")
    mm = datetime.datetime.now().strftime("%M")
    ss = datetime.datetime.now().strftime("%S")
    return now_time, hour, mm, ss


def sleep_time(hour, m, sec):
    """返回总共秒数"""
    return hour * 3600 + m * 60 + sec


def send_msg(content):
    """艾特全部,并发送指定信息"""
    data = json.dumps({"msgtype": "text", "text": {"content": content, "mentioned_list":["@all"]}})
    r = requests.post(wx_url, data, auth=('Content-Type', 'application/json'))
    print(r.json)


def every_time_send_msg(interval_h=0, interval_m=0, interval_s=2, special_h="00", special_m="00", mode="special"):
    """每天指定时间发送指定消息"""

    # 设置自动执行间隔时间
    second = sleep_time(interval_h, interval_m, interval_s)
    # 死循环
    while 1 == 1:
        # 获取当前时间和当前时分秒
        c_now, c_h, c_m, c_s = get_current_time()
        print("当前时间:", c_now, c_h, c_m, c_s)
        if mode == "special":
            if c_h == special_h and c_m == special_m:
                # 执行
                print("正在发送...")
                send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
                headers = {"Content-Type": "text/plain"}
                send_data = {
                    "msgtype": "markdown",
                    "markdown": {
                        "content": "[BOWWOB的博客](https://blog.csdn.net/BOWWOB?spm=1001.2101.3001.5343)实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n>类型:<font color=\"comment\">用户反馈</font>>普通用户反馈:<font color=\"comment\">117例</font>>VIP用户反馈:<font color=\"comment\">15例</font>"
                    }
                }
                requests.post(url=send_url, headers=headers, json=send_data)
        else:
            send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
            headers = {"Content-Type": "text/plain"}
            send_data = {
                "msgtype": "markdown",
                "markdown": {
                    "content": "[BOWWOB的博客](https://blog.csdn.net/BOWWOB?spm=1001.2101.3001.5343)实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n>类型:<font color=\"comment\">用户反馈</font>>普通用户反馈:<font color=\"comment\">117例</font>>VIP用户反馈:<font color=\"comment\">15例</font>"
                }
            }
            requests.post(url=send_url, headers=headers, json=send_data)
        print("每隔" + str(interval_h) + "小时" + str(interval_m) + "分" + str(interval_s) + "秒执行一次")
        # 延时
        time.sleep(second)


if __name__ == '__main__':
    every_time_send_msg(mode="no")

七、周期性-发送图片

#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create type_time: 2020-2-24
Info: 定期向企业微信推送消息
"""
import requests, json
import datetime
import time

import base64
import hashlib
wx_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="    # 测试机器人1号

def get_current_time():
    """获取当前时间,当前时分秒"""
    now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    hour = datetime.datetime.now().strftime("%H")
    mm = datetime.datetime.now().strftime("%M")
    ss = datetime.datetime.now().strftime("%S")
    return now_time, hour, mm, ss


def sleep_time(hour, m, sec):
    """返回总共秒数"""
    return hour * 3600 + m * 60 + sec


def send_msg(content):
    """艾特全部,并发送指定信息"""
    data = json.dumps({"msgtype": "text", "text": {"content": content, "mentioned_list":["@all"]}})
    r = requests.post(wx_url, data, auth=('Content-Type', 'application/json'))
    print(r.json)


def every_time_send_msg(interval_h=0, interval_m=0, interval_s=5, special_h="00", special_m="00", mode="special"):
    """每天指定时间发送指定消息"""
    with open('E:\\py_image1.png', 'rb') as file:  # 转换图片成base64格式
        data = file.read()
        encodestr = base64.b64encode(data)
        image_data = str(encodestr, 'utf-8')

    with open('E:\\py_image1.png', 'rb') as file:  # 图片的MD5值
        md = hashlib.md5()
        md.update(file.read())
        image_md5 = md.hexdigest()
    # 设置自动执行间隔时间
    second = sleep_time(interval_h, interval_m, interval_s)
    # 死循环
    while 1 == 1:
        # 获取当前时间和当前时分秒
        c_now, c_h, c_m, c_s = get_current_time()
        print("当前时间:", c_now, c_h, c_m, c_s)
        if mode == "special":
            if c_h == special_h and c_m == special_m:
                # 执行
                print("正在发送...")
                url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="  # 填上机器人Webhook地址
                headers = {"Content-Type": "application/json"}
                data = {
                    "msgtype": "image",
                    "image": {
                        "base64": image_data,
                        "md5": image_md5
                    }
                }

                result = requests.post(url, headers=headers, json=data)
        else:
            url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="  # 填上机器人Webhook地址
            headers = {"Content-Type": "application/json"}
            data = {
                "msgtype": "image",
                "image": {
                    "base64": image_data,
                    "md5": image_md5
                }
            }

            result = requests.post(url, headers=headers, json=data)
        print("每隔" + str(interval_h) + "小时" + str(interval_m) + "分" + str(interval_s) + "秒执行一次")
        # 延时
        time.sleep(second)


if __name__ == '__main__':
    every_time_send_msg(mode="no")
wx_image('E:\\py_image1.png')  # 传入图片路径

八、周期性-发送图文(图文各为一个超链接)

#! -*- coding: utf-8 -*-
"""
Author: ZhenYuSha
Create type_time: 2020-2-24
Info: 定期向企业微信推送消息
"""
import requests, json
import datetime
import time

wx_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="    # 测试机器人1号
send_message = "测试:测试机器人1号………………………………!"

def get_current_time():
    """获取当前时间,当前时分秒"""
    now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    hour = datetime.datetime.now().strftime("%H")
    mm = datetime.datetime.now().strftime("%M")
    ss = datetime.datetime.now().strftime("%S")
    return now_time, hour, mm, ss


def sleep_time(hour, m, sec):
    """返回总共秒数"""
    return hour * 3600 + m * 60 + sec


def send_msg(content):
    """艾特全部,并发送指定信息"""
    data = json.dumps({"msgtype": "text", "text": {"content": content, "mentioned_list":["@all"]}})
    r = requests.post(wx_url, data, auth=('Content-Type', 'application/json'))
    print(r.json)

def every_time_send_msg(interval_h=0, interval_m=0, interval_s=2, special_h="00", special_m="00", mode="special"):
    """每天指定时间发送指定消息"""

    # 设置自动执行间隔时间
    second = sleep_time(interval_h, interval_m, interval_s)
    # 死循环
    while 1 == 1:
        # 获取当前时间和当前时分秒
        c_now, c_h, c_m, c_s = get_current_time()
        print("当前时间:", c_now, c_h, c_m, c_s)
        if mode == "special":
            if c_h == special_h and c_m == special_m:
                # 执行
                print("正在发送...")
                headers = {"Content-Type": "text/plain"}
                send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
                send_data = {
                    "msgtype": "news",  # 消息类型,此时固定为news
                    "news": {
                        "articles": [  # 图文消息,一个图文消息支持1到8条图文
                            {
                                "title": "中秋节礼品领取",  # 标题,不超过128个字节,超过会自动截断
                                "description": "今年中秋节公司有豪礼相送",  # 描述,不超过512个字节,超过会自动截断
                                "url": "www.baidu.com",  # 点击后跳转的链接。
                                "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                                # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                            },
                            {
                                "title": "我的CSDN - 魏风物语",  # 标题,不超过128个字节,超过会自动截断
                                "description": "坚持每天写一点点",  # 描述,不超过512个字节,超过会自动截断
                                "url": "https://blog.csdn.net/BOWWOB",  # 点击后跳转的链接。
                                "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                                # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                            }
                        ]
                    }
                }
                res = requests.post(url=send_url, headers=headers, json=send_data)
        else:
            headers = {"Content-Type": "text/plain"}
            send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
            send_data = {
                "msgtype": "news",  # 消息类型,此时固定为news
                "news": {
                    "articles": [  # 图文消息,一个图文消息支持1到8条图文
                        {
                            "title": "中秋节礼品领取",  # 标题,不超过128个字节,超过会自动截断
                            "description": "今年中秋节公司有豪礼相送",  # 描述,不超过512个字节,超过会自动截断
                            "url": "www.baidu.com",  # 点击后跳转的链接。
                            "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                            # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                        },
                        {
                            "title": "我的CSDN - 魏风物语",  # 标题,不超过128个字节,超过会自动截断
                            "description": "坚持每天写一点点",  # 描述,不超过512个字节,超过会自动截断
                            "url": "https://blog.csdn.net/BOWWOB",  # 点击后跳转的链接。
                            "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                            # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                        }
                    ]
                }
            }
            res = requests.post(url=send_url, headers=headers, json=send_data)

            print("每隔" + str(interval_h) + "小时" + str(interval_m) + "分" + str(interval_s) + "秒执行一次")
        # 延时
        time.sleep(second)


# if __name__ == '__main__':
#     every_time_send_msg(mode="no")


class WXWork_SMS:

    # 图文类型消息
    def send_msg_txt_img(self):
        headers = {"Content-Type": "text/plain"}
        send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
        send_data = {
            "msgtype": "news",  # 消息类型,此时固定为news
            "news": {
                "articles": [  # 图文消息,一个图文消息支持1到8条图文
                    {
                        "title": "中秋节礼品领取",  # 标题,不超过128个字节,超过会自动截断
                        "description": "今年中秋节公司有豪礼相送",  # 描述,不超过512个字节,超过会自动截断
                        "url": "www.baidu.com",  # 点击后跳转的链接。
                        "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                        # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                    },
                    {
                        "title": "我的CSDN - 魏风物语",  # 标题,不超过128个字节,超过会自动截断
                        "description": "坚持每天写一点点",  # 描述,不超过512个字节,超过会自动截断
                        "url": "https://blog.csdn.net/BOWWOB",  # 点击后跳转的链接。
                        "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                        # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                    }
                ]
            }
        }

        res = requests.post(url=send_url, headers=headers, json=send_data)
        print(res.text)

if __name__ == '__main__':
    # sms = WXWork_SMS()
    # # 图文类型消息
    # sms.every_time_send_msg(mode="no")
    every_time_send_msg(mode="no")

九、每天定时-发送content

from threading import Timer
import requests
import random
import time
import schedule


# 获取金山词霸每日一句英文和翻译
def get_news():
    url = "http://open.iciba.com/dsapi/"
    res = requests.get(url)
    content = res.json()['content']
    note = res.json()['note']
    translation = res.json()['translation']
    translation = translation.replace('小编的话', '进击物语')
    return content, note, translation

# 文本类型消息
def send_msg_txt(content):
    send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="

    headers = {"Content-Type": "text/plain"}
    send_data = {
        "msgtype": "text",  # 消息类型,此时固定为text
        "text": {
            "content": "123"  # 文本内容,最长不超过2048个字节,必须是utf8编码
        }
    }

    requests.post(url=send_url, headers=headers, json=send_data)




def send_job():
    contents = get_news()

#原来打印的时候是一段话 一句句打印 才要延时,不会影响定时。
    for text in contents:
        rand_sec = random.randint(1, 5)  # 利用随机数让前后消息发送的时间产生延时
        time.sleep(rand_sec)
        # 打印下发送内容
        print("当前时间:%s,消息内容:%s" % (time.ctime(), text))
        send_msg_txt(text)


# 定时每天某个时刻执行一次job函数
schedule.every().day.at("19:14").do(send_job)

while True:
    schedule.run_pending()  # 确保schedule一直运行
    time.sleep(2)

十、每天定时-支持发送markdown文本

from threading import Timer
import requests
import random
import time
import schedule

# 文本类型消息
def send_msg_txt():
    send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
    headers = {"Content-Type": "text/plain"}
    send_data = {
    "msgtype": "markdown",
    "markdown": {
        "content": "[BOWWOB的博客](https://blog.csdn.net/BOWWOB?spm=1001.2101.3001.5343)实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n>类型:<font color=\"comment\">用户反馈</font>>普通用户反馈:<font color=\"comment\">117例</font>>VIP用户反馈:<font color=\"comment\">15例</font>"
    }
}

    requests.post(url=send_url, headers=headers, json=send_data)

def send_job():
        send_msg_txt()

# 定时每天某个时刻执行一次job函数
schedule.every().day.at("20:52").do(send_job)

while True:
    schedule.run_pending()  # 确保schedule一直运行
    time.sleep(2)

十一、每天定时-发送图片

from threading import Timer
import requests
import random
import time
import schedule
import base64
import hashlib

def wx_image(image):
    with open(image, 'rb') as file:  # 转换图片成base64格式
        data = file.read()
        encodestr = base64.b64encode(data)
        image_data = str(encodestr, 'utf-8')

    with open(image, 'rb') as file:  # 图片的MD5值
        md = hashlib.md5()
        md.update(file.read())
        image_md5 = md.hexdigest()

    url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="  # 填上机器人Webhook地址
    headers = {"Content-Type": "application/json"}
    data = {
        "msgtype": "image",
        "image": {
            "base64": image_data,
            "md5": image_md5
        }
    }
    result = requests.post(url, headers=headers, json=data)
    return result
def send_job():
    wx_image('E:\\py_image1.png')

# 定时每天某个时刻执行一次job函数
schedule.every().day.at("19:32").do(send_job)

while True:
    schedule.run_pending()  # 确保schedule一直运行
    time.sleep(2)

十二、每天定时-发送图文(图文各为一个超链接)

from threading import Timer
import requests
import random
import time
import schedule

# 图文类型消息
def send_msg_txt_img():
        headers = {"Content-Type": "text/plain"}
        send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
        send_data = {
            "msgtype": "news",  # 消息类型,此时固定为news
            "news": {
                "articles": [  # 图文消息,一个图文消息支持1到8条图文
                    {
                        "title": "中秋节礼品领取",  # 标题,不超过128个字节,超过会自动截断
                        "description": "今年中秋节公司有豪礼相送",  # 描述,不超过512个字节,超过会自动截断
                        "url": "www.baidu.com",  # 点击后跳转的链接。
                        "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                        # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                    },

                    {
                        "title": "我的CSDN - 魏风物语",  # 标题,不超过128个字节,超过会自动截断
                        "description": "坚持每天写一点点",  # 描述,不超过512个字节,超过会自动截断
                        "url": "https://blog.csdn.net/BOWWOB",  # 点击后跳转的链接。
                        "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                        # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。

                    }
                ]
            }
        }

        res = requests.post(url=send_url, headers=headers, json=send_data)
        print(res.text)



def send_job():
    send_msg_txt_img();


#记得do()方法里面的方法不要写括号
#记得do()方法里面的方法不要写括号
#记得do()方法里面的方法不要写括号
#记得do()方法里面的方法不要写括号
#记得do()方法里面的方法不要写括号
#记得do()方法里面的方法不要写括号    # 定时每天某个时刻执行一次job函数
schedule.every().day.at("19:42").do(send_job)

while True:
    schedule.run_pending()  # 确保schedule一直运行
    time.sleep(2)

十三、延时(倒计时)-发送普通文本

import threading
from threading import Timer
import requests
import random
import time
import schedule

def hello():
    send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="

    headers = {"Content-Type": "text/plain"}
    send_data = {
        "msgtype": "text",  # 消息类型,此时固定为text
        "text": {
            "content": "123"  # 文本内容,最长不超过2048个字节,必须是utf8编码
        }
    }

    requests.post(url=send_url, headers=headers, json=send_data)


# 指定2秒后执行hello函数
t = threading.Timer(2, hello)
t.start()

十四、延时(倒计时)-发送图Markdown文本

import threading
from threading import Timer
import requests
import random
import time
import schedule

def hello():
    send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="

    send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
    headers = {"Content-Type": "text/plain"}
    send_data = {
        "msgtype": "markdown",
        "markdown": {
            "content": "[BOWWOB的博客](https://blog.csdn.net/BOWWOB?spm=1001.2101.3001.5343)实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n>类型:<font color=\"comment\">用户反馈</font>>普通用户反馈:<font color=\"comment\">117例</font>>VIP用户反馈:<font color=\"comment\">15例</font>"
        }
    }
    requests.post(url=send_url, headers=headers, json=send_data)


# 指定2秒后执行hello函数
t = threading.Timer(2, hello)
t.start()

十五、延时(倒计时)-发送图片

import threading
from threading import Timer
import requests
import random
import time
import schedule
import requests
import base64
import hashlib

def hello():
    with open('E:\\py_image1.png', 'rb') as file:  # 转换图片成base64格式
        data = file.read()
        encodestr = base64.b64encode(data)
        image_data = str(encodestr, 'utf-8')

    with open('E:\\py_image1.png', 'rb') as file:  # 图片的MD5值
        md = hashlib.md5()
        md.update(file.read())
        image_md5 = md.hexdigest()

    url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="  # 填上机器人Webhook地址
    headers = {"Content-Type": "application/json"}
    data = {
        "msgtype": "image",
        "image": {
            "base64": image_data,
            "md5": image_md5
        }
    }

    result = requests.post(url, headers=headers, json=data)



# 指定2秒后执行hello函数
t = threading.Timer(2, hello)
t.start()

十六、延时(倒计时)-发送图文(图文各为一个超链接)

import threading
from threading import Timer
import requests
import random
import time
import schedule
import requests
import json
def hello():
    headers = {"Content-Type": "text/plain"}
    send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key="
    send_data = {
        "msgtype": "news",  # 消息类型,此时固定为news
        "news": {
            "articles": [  # 图文消息,一个图文消息支持1到8条图文
                {
                    "title": "中秋节礼品领取",  # 标题,不超过128个字节,超过会自动截断
                    "description": "今年中秋节公司有豪礼相送",  # 描述,不超过512个字节,超过会自动截断
                    "url": "www.baidu.com",  # 点击后跳转的链接。
                    "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                    # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                },
                {
                    "title": "我的CSDN - 魏风物语",  # 标题,不超过128个字节,超过会自动截断
                    "description": "坚持每天写一点点",  # 描述,不超过512个字节,超过会自动截断
                    "url": "https://blog.csdn.net/BOWWOB",  # 点击后跳转的链接。
                    "picurl": "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                    # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
                }
            ]
        }
    }

    res = requests.post(url=send_url, headers=headers, json=send_data)


# 指定2秒后执行hello函数
t = threading.Timer(2, hello)
t.start()


总结

提示:这里对文章进行总结:
以上就是今天要讲的内容

  • 6
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值