企业飞书应用机器人,使用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",
    )

使用的效果:

相关推荐
没有bug.的程序员21 分钟前
服务安全:内部服务如何防止“裸奔”?
java·网络安全·云原生安全·服务安全·零信任架构·微服务安全·内部鉴权
一线大码38 分钟前
SpringBoot 3 和 4 的版本新特性和升级要点
java·spring boot·后端
weixin_440730501 小时前
java数组整理笔记
java·开发语言·笔记
weixin_425023001 小时前
Spring Boot 实用核心技巧汇总:日期格式化、线程管控、MCP服务、AOP进阶等
java·spring boot·后端
一线大码1 小时前
Java 8-25 各个版本新特性总结
java·后端
2501_906150561 小时前
私有部署问卷系统操作实战记录-DWSurvey
java·运维·服务器·spring·开源
better_liang2 小时前
每日Java面试场景题知识点之-TCP/IP协议栈与Socket编程
java·tcp/ip·计算机网络·网络编程·socket·面试题
niucloud-admin2 小时前
java服务端——controller控制器
java·开发语言
To Be Clean Coder2 小时前
【Spring源码】通过 Bean 工厂获取 Bean 的过程
java·后端·spring
Fortunate Chen2 小时前
类与对象(下)
java·javascript·jvm