吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2046|回复: 11
收起左侧

[Python 转载] Python实现向feishu发消息

[复制链接]
js20184 发表于 2022-8-15 18:31
使用Python 可以向飞书发送消息,可以将任务插在程序之中,可以进行报警、异常提醒等动作

一、引用的程序包
[Python] 纯文本查看 复制代码
import json
import requests
import logging
import hashlib
import base64
import hmac
import time

二、编写函数
[Python] 纯文本查看 复制代码
def gen_sign(timestamp, secret):
    # 拼接timestamp和secret
    string_to_sign = f'{timestamp}\n{secret}'
    hmac_code = hmac.new(string_to_sign.encode("utf-8"), digestmod=hashlib.sha256).digest()
    # 对结果进行base64处理
    sign = base64.b64encode(hmac_code).decode('utf-8')
    return sign

[Python] 纯文本查看 复制代码
def send_fei_msg(webhook, msg, sign, timestamp, title):

    """发送text类型飞书消息通知"""

    # 构造钉钉消息头,必须制定utf-8编码
    headers = {"Content-Type": "application/json;charset=utf-8"}
    # 构造钉钉消息体,拷贝text消息格式
    msg_body = {
            "timestamp": timestamp,
            "sign": sign,
            "msg_type": "post",
            "content": {
                "post": {
                    "zh_cn": {
                        "title": title,
                        "content": [
                            [{
                                "tag": "text",
                                "text": msg
                            }
                            ]
                        ]
                    }
                }
            }
        }
    # 向hook地址发送post请求
    try:
        res = requests.post(webhook, data=json.dumps(msg_body), headers=headers)
        res = res.json()
    except Exception as e:
        logging.error("请求异常: %s" % e)
        res = None

    return res

三、具体使用方法
[Python] 纯文本查看 复制代码
if __name__ == '__main__':
    ticks = time.time()
    timestamp = int(ticks)
    access_token = "群机器人的token"
    webhook = f"https://open.feishu.cn/open-apis/bot/v2/hook/{access_token}"
    msg = "我是机器人"
    title = "项目名称"
    secret = '校验码'
    sign = gen_sign(timestamp, secret)
    s = send_fei_msg(webhook, msg, sign, timestamp, title)

免费评分

参与人数 9吾爱币 +13 热心值 +9 收起 理由
f2arayner + 1 + 1 谢谢@Thanks!
shadmmd + 1 谢谢@Thanks!
坐久落花多 + 1 + 1 能直接js或者油猴脚本实现么?
43991258 + 1 + 1 谢谢@Thanks!
moke706 + 1 我很赞同!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
skywalker0123 + 2 + 1 我很赞同!
wawai + 1 + 1 我很赞同!
三滑稽甲苯 + 2 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

tencentma 发表于 2022-8-15 19:37
我看楼主写的详细,我也试试看能否向飞书发消息
wawai 发表于 2022-8-15 19:43
trouman 发表于 2022-8-15 21:29
communistzhao 发表于 2022-8-15 21:45
感谢楼主,受教了。十分感谢
lansemeiying 发表于 2022-8-15 22:44
我也来试试看
hnwang 发表于 2022-8-16 07:32
感谢分享 学习一波
zhacai 发表于 2022-8-16 08:07
这个有技术含量,可以慢慢研究下。
ilulyj 发表于 2022-8-16 08:27
正在学习python,可惜进展很慢,楼主的代码很漂亮
43991258 发表于 2022-8-16 09:23
谢谢大佬分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-7 05:51

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表