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

使用的效果:

相关推荐
Aaswk22 分钟前
Java Lambda 表达式与流处理
java·开发语言·python
是宇写的啊32 分钟前
Spring AOP
java·spring
辰尘_星启35 分钟前
【ROS2】DDS 底层通信协议与数据流转全链路解析
机器人·ros2
万邦科技Lafite41 分钟前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
Mr_pyx2 小时前
Spring AI 入门教程:Java开发者的AI应用捷径
java·人工智能·spring
Zephyr_02 小时前
Leedcode算法题
java·算法
苍煜3 小时前
Java开发IO零基础吃透:BIO、NIO、同步异步、阻塞非阻塞
java·python·nio
折哥的程序人生 · 物流技术专研3 小时前
Java面试85题图解版(一):基础核心篇
java·开发语言·后端·面试
AllData公司负责人4 小时前
通过Postgresql同步到Doris,全视角演示AllData数据中台核心功能效果,涵盖:数据入湖仓,数据同步,数据处理,数据服务,BI可视化驾驶舱
java·大数据·数据库·数据仓库·人工智能·python·postgresql
飞Link4 小时前
具身智能港亮相深圳:从“大脑”到“身体”,开启人形机器人产业新纪元
人工智能·机器人