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.youkuaiyun.com/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": "我的优快云 - 魏风物语",  # 标题,不超过128个字节,超过会自动截断
                        "description": "坚持每天写一点点",  # 描述,不超过512个字节,超过会自动截断
                        "url": "https://blog.youkuaiyun.com/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("每隔" + 
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值