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

使用的效果:

相关推荐
EXI-小洲1 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
2601_961901702 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
何以解忧,唯有..3 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
QQ_21696290963 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战
java·spring boot·后端
前端 贾公子5 小时前
第09章:上下文与记忆 (2)
java·服务器·前端
2601_962203515 小时前
Java进阶07集合(续)
java·开发语言
郑州光合科技余经理5 小时前
本地生活服务系统:模块边界与结算字段怎么拆
java·开发语言·前端·后端·系统架构·uni-app·php
wjkjpcba5 小时前
高密度SMT贴片代工厂:机器人主板的高密度贴片难点
机器人·smt贴片加工厂·pcba贴片加工·智能机器人pcba
沫儿笙5 小时前
弧焊机器人智能节气阀
人工智能·机器人
roman_日积跬步-终至千里5 小时前
【数据治理(6)】DataOps 不是调度工具,而是让数据产品长期可信的运行机制
java·服务器·网络