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

文章目录

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

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

  2. 参考官方文档,获取两个参数:app_id与app_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",
    )

使用的效果:

相关推荐
shaibdoio1 分钟前
AI主动获客的意图识别工程实现:从规则匹配到模型微调
java·开发语言·人工智能·长沙瞬维ai
for_ever_love__14 分钟前
MySQL 事务隔离级别讲透:MVCC、幻读与四个级别怎么选
java·数据库·mysql·事务·mvcc·不可重复读·幻读
CoderYanger1 小时前
一轮复习——F.动态规划模型总结(入门篇)
java·开发语言·数据结构·算法·leetcode·职场和发展·动态规划
她的男孩1 小时前
打印模板草稿能保存,一点发布就报主从关系:我们把校验拆成了两档
java·spring boot·后端
小鹿的周先生1 小时前
第19章-Agent
java·spring·ai
溪语流沙1 小时前
【Web全栈进阶】FastAPI工程化:APIRouter拆分 + 配置 + 依赖注入
java·前端·fastapi
写后端的胖头鱼1 小时前
Redis 的 RDB 和 AOF
java·数据库·redis
cxoptics1 小时前
LBO 激光损伤阈值 LIDT 典型数值?如何避免晶体打坏?
java
逐米时代1 小时前
远程运维诊断减少到场率
java·服务器·前端
禾小西1 小时前
Redis AOF 日志:宕机了,如何避免数据丢失?
java·redis·mybatis