Skip to main content

Feishu Third-party Libraries

Project description

飞书 sdk

第三方Feishu库,目前只实现了使用的机器人部分功能,后续会看精力继续完善

安装

pip install feishu-sdk

调试

将日志调到debug级会显示更多信息

import feishu
feishu.set_log_level('debug')

导入

from feishu import Bot

bot = Bot(app_id='xxxxxxxxxxxxxxx', app_secret="xxxxxxxxxxxxxxx")

事前准备

测试的话需要给自己发送消息,我们需要一个飞书的登录预授权码

注意有效期仅为5分钟

你需要在飞书开放平台 所测试的机器人 -> 安全设置中 添加一个重定向URL,内容为https://example.com

使用下面方法或手动拼接,获取一个地址,类似于https://open.feishu.cn/open-apis/authen/v1/index?app_id=xxxxxxxxx&redirect_uri=https://example.com&state= ,在浏览器中打开,登录后,从地址栏中拿到预授权码

print(bot.get_authorization_code_by_browser_url())

然后获取你自己的 open_id

print(bot.get_user_info(code='上面获取的预授权码'))

部分功能实例

获取Token

# 租户Token
print(bot.get_tenant_access_token())
# app Token
print(bot.get_app_access_token())

#返回示例 (请注意,token的expire字段是在官方返回失效时间基础上计算出的具体的失效时间戳)
##tenant_access_token
{'code': 0, 'expire': 1592468406.488201, 'msg': 'ok', 'tenant_access_token': 't-be56a33ef75ce4bb867ccc84ac0da5fdf8017c2b'}
##app_access_token
{'app_access_token': 't-be56a33ef75ce4bb867ccc84ac0da5fdf8017c2b', 'code': 0, 'expire': 1592468406.551423, 'msg': 'ok', 'tenant_access_token': 't-be56a33ef75ce4bb867ccc84ac0da5fdf8017c2b'}

创建群组

bot.create_group(name="测试群组", open_ids=['xxxxxxx'])

alt

#返回示例
{'code': 0, 'data': {'chat_id': 'oc_4ebe54802633fa661305f06ec7a99cb0', 'invalid_open_ids': [], 'invalid_user_ids': []}, 'msg': 'ok'}

发送文字消息

bot.send_user_message(user_open_id="ou_b7fd6a20da4e3903bc2324b71232c5ac", text='Hi,feishu~')

alt

#返回示例
{'code': 0, 'data': {'message_id': 'om_74cde4875127f7e39a47153d59d508be'}, 'msg': 'ok'}

发送卡片消息(默认样式)

bot.send_user_card_message(user_open_id="ou_b7fd6a20da4e3903bc2324b71232c5ac", title="测试标题", text="测试内容")

alt

发送通知卡片消息(基于飞书markdown)

此功能初衷是为了实现消息通知更美观一些,故样式针对样式做了一点调整。

请注意,飞书卡片消息并非支持所有Markdown语法,请参考文档

message_config = {
            'title': '上线任务审批通知',
            'action': '上线审批',
            'system': ','.join(['a', 'b', 'c']),
            'reason': '因为地球毁灭',
            'detail': '用户:xxxx 申请发布服务,您可以确认。',
        }
bot.send_user_card_message(user_open_id="ou_b7fd6a20da4e3903bc2324b71232c5ac", **message_config)

alt

#返回数据格式
{'code': 0, 'data': {'message_id': 'om_b3747313d4f46a9e61067461cc24955a'}, 'msg': 'ok'}

你可能会好奇为什么你传入的是action,但在通知时会转换成中文操作,因为为了使用方便已经将一些常用的业务场景关键参数做了映射,你可以做如下操作自定义

from feishu import Bot
Bot.MESSAGE_MAP={
    'your key':'your value'
}

当没有匹配到关键词时,将会使用你的参数名做默认显示。 例如

message_config = {
    'title': '上线任务审批通知',
    'who are you': "I'm Iron Man",
}
bot.send_user_card_message(user_open_id="ou_b7fd6a20da4e3903bc2324b71232c5ac", **message_config)

alt

消息卡片增加按钮(button)

这里对消息卡片的button模块做了简单的封装,你可以从官方文档获得更详细的资料

你只需要传入button字段即可,列表内可写多个button。你需要在button中的value字段写入自己业务需要的信息。更多交互详情请参考上面官方文档链接。

message_config = {
            'title': '上线任务审批通知',
            'action': '上线审批',
            'system': ','.join(['a', 'b', 'c']),
            'reason': '因为地球毁灭',
            'detail': '用户:xxxx 申请发布服务,您可以确认。',
            'button': [
                {
                    'title': '同意',
                    'type': 'primary',
                    'value': {
                        'action': 'approval_online_order',
                        'order_id': '12345',
                        'option': 'agree'
                    },
                },
                {
                    'title': '拒绝',
                    'type': 'danger',
                    'value': {
                        'action': 'approval_online_order',
                        'order_id': '12345',
                        'user_id': '123',
                        'option': 'disagree'
                    }
                },
            ]
        }
bot.send_user_card_message(user_open_id="ou_b7fd6a20da4e3903bc2324b71232c5ac", **message_config)

alt

#返回数据格式
{'code': 0, 'data': {'message_id': 'om_b3747313d4f46a9e61067461cc24955a'}, 'msg': 'ok'}

卡片消息格式自定义

这里支持对卡片消息做更详细的调整

参数名 说明
image_key 飞书图片的image_key 可增加卡片消息图标,增进正式感。image_key获取方法参考文档
button_confirm True/False button是否需要二次确认
name str 卡片通知者名称,默认为:通知
auto_add_time True/False 自动增加通知时间,为True时,会在内容自动增加一行
bot.configuration_card_message(image_key="img_5abe5193-14df-4b7d-af6f-15fac38c485g", 
                               button_confirm=False, 
                               name='钢铁侠',
                               auto_add_time=True)

bot.send_user_card_message(user_open_id="ou_b7fd6a20da4e3903bc2324b71232c5ac", **message_config)

alt

#返回数据格式
{'code': 0, 'data': {'message_id': 'om_b3747313d4f46a9e61067461cc24955a'}, 'msg': 'ok'}

其他

  • 建议使用Python3,Python2会去做兼容,但不保证。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

feishu-sdk-1.0.3.tar.gz (9.2 kB view hashes)

Uploaded Source

Built Distribution

feishu_sdk-1.0.3-py3-none-any.whl (10.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page