飞书机器人/应用开发

本教程均已发送到指定群聊为例
开发之前首先需进入飞书开放平台注册应用
飞书开放平台
拿到应用的 APP_IDAPP_SECRET

1. 获取应用的access_token

def get_access_token(APP_ID, APP_SECRET):
    url = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal'
    headers = {
        'Content-Type': "application/json; charset=utf-8"
    }
    data = {
        'app_id': APP_ID,
        'app_secret': APP_SECRET
    }
    res = requests.post(url, headers=headers, json=data).json()
    return res['tenant_access_token']

2. 获取发送目的地的id(如用户id)

可选择根据用户邮箱或者手机号查找,也可同时使用

def get_user_id(token):
    url = 'https://open.feishu.cn/open-apis/contact/v3/users/batch_get_id?user_id_type=union_id'
    headers = {
        'Authorization': "Bearer " + token,
        'Content-Type': "application/json; charset=utf-8"
    }
    # data = {
    #     'user_id_type': 'union_id',
    # }
    json = {
        # "emails": [
        #     "[email protected]", "[email protected]"
        # ],
        "mobiles": [
            "13372947224",
        ]
    }
    res = requests.post(url, headers=headers, json=json).json()
    return res

3. 获取群聊id

获取之前开启应用的机器人能力,并把机器人拉入相关群聊

# 该函数会返回机器人所在的所有群聊
def get_chat_id(token):
    url = 'https://open.feishu.cn/open-apis/im/v1/chats'
    headers = {
        'Authorization': "Bearer " + token,
        'Content-Type': "application/json; charset=utf-8"
    }
    res = requests.get(url, headers=headers).json()
    return res

4. 发送消息或消息卡片

本例发送消息可采用两种方式

  • 当types=1时,利用群聊创建的自定义机器人发送,bot为机器人的webhook
  • 当types=2时,利用在开发者后台创建的应用发送消息,receive_id为接受群聊的chat_id, token为前面获取的应用的access_token
def send_text(content, receive_id='', token='', bot='', types=1):
    # 发送消息
    if types == 1:
        url = 'https://open.feishu.cn/open-apis/bot/v2/hook/' + bot
        headers = {"Content-Type": "text/plain"}
        data = {
            "msg_type": "text",
            "content": {
                "text": content
            }
        }
        r = requests.post(url, headers=headers, json=data)
        return r.text
    # 发送消息
    elif types == 2:
        url = 'https://open.feishu.cn/open-apis/im/v1/messages/'
        params = {
            'receive_id_type': 'chat_id'
        }
        headers = {
            'Authorization': "Bearer {}".format(token),
            "Content-Type": "application/json; charset=utf-8"
        }
        data = {
            "receive_id": receive_id,
            "msg_type": "text",
            "content": content,
        }
        r = requests.post(url=url, params=params, headers=headers, data=json.dumps(data))
    # 发送消息卡片
    elif types == 3:
        # open_id user_id union_id email chat_id
        url = 'https://open.feishu.cn/open-apis/im/v1/messages/'
        params = {
            'receive_id_type': 'chat_id'
        }
        headers = {
            'Authorization': "Bearer {}".format(token),
            "Content-Type": "application/json; charset=utf-8"
        }
        data = {
            "receive_id": receive_id,
            "msg_type": "interactive",
            "content": content,
        }
        r = requests.post(url=url, params=params, headers=headers, data=json.dumps(data))
        print('res', r, r.text)
        return r.text

上传图片(其他类似)

有时候会有涉及到文件上传,比如消息卡片中嵌套的图片,此时通过创建的应用即可

def upload_img(img_path, token):
    resp = requests.post(
        url='https://open.feishu.cn/open-apis/image/v4/put/',
        headers={'Authorization': "Bearer " + token},
        files={
            "image": open(img_path, "rb")
        },
        data={
            "image_type": "message"
        },
        stream=True)
    resp.raise_for_status()
    content = resp.json()
    if content.get("code") == 0:
        return content['data']['image_key']
    else:
        return Exception("Call Api Error, errorCode is %s" % content["code"])
  • token 为应用的access_token
  • img_path 为图片路径

你可能感兴趣的:(RPA,机器人,python,飞书机器人,自动化)