企业飞书应用机器人,使用python自动发送文字内容到群消息

文章目录

创建企业应用与开通机器人

  1. 需要先创建应用,然后进入应用后,点击添加应用能力创建机器人:

  2. 参考官方文档,获取两个参数:app_idapp_secret

飞书发送信息的工具函数

博主写了下面的函数,作为发送基本的文本的工具类,其中用到的参数有:

  1. text_string:发送的文字字符串
  2. chat_id:群的ID,在API调试台可以直接获取(https://open.feishu.cn/api-explorer/
  3. app_id:参考官方文档,获取企业应用的app_id
  4. app_secret:参考官方文档,获取企业应用的app_secret
py 复制代码
import lark_oapi as lark
from lark_oapi.api.im.v1 import *


def fs_send_message(text_string: str, chat_id, app_id=None, app_secret=None):
    """飞书发送基本的文本信息 工具类"""
    client = lark.Client.builder().app_id(app_id).app_secret(app_secret).log_level(lark.LogLevel.DEBUG).build()
    send_text_string = {"text": text_string}
    # 构造请求对象
    request: CreateMessageRequest = CreateMessageRequest.builder() \
        .receive_id_type("chat_id").request_body(
        CreateMessageRequestBody.builder().receive_id(chat_id).msg_type("text").content(
            str(send_text_string).replace("'", "\"")).build()).build()
    # 发起请求
    response: CreateMessageResponse = client.im.v1.message.create(request)
    # 处理失败返回
    if not response.success():
        lark.logger.error(
            f"client.im.v1.message.create failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_log_id()}")
        return

    # 处理业务结果
    lark.logger.info(lark.JSON.marshal(response.data, indent=4))

使用方法:

py 复制代码
fs_send_message(
        text_string="发送的测试文字内容:balabala",
        chat_id="oc_xxxxxx",
        app_id="cli_xxxx",
        app_secret="xxxxx",
    )

使用的效果:

相关推荐
Z3r4y1 分钟前
【代码审计】RuoYi-4.7.3&4.7.8 定时任务RCE 漏洞分析
java·web安全·ruoyi·代码审计
Kuo-Teng1 小时前
LeetCode 160: Intersection of Two Linked Lists
java·算法·leetcode·职场和发展
Jooou1 小时前
Spring事务实现原理深度解析:从源码到架构全面剖析
java·spring·架构·事务
盖世英雄酱581362 小时前
commit 成功为什么数据只更新了部分?
java·数据库·后端
ZPC82102 小时前
FPGA 部署ONNX
人工智能·python·算法·机器人
码上淘金3 小时前
在 YAML 中如何将 JSON 对象作为字符串整体赋值?——兼谈 Go Template 中的 fromJson 使用
java·golang·json
刘一说3 小时前
Spring Boot 应用的指标收集与监控体系构建指南
java·spring boot·后端
老友@3 小时前
Java Excel 导出:EasyExcel 使用详解
java·开发语言·excel·easyexcel·excel导出
Full Stack Developme4 小时前
java.net.http 包详解
java·http·.net