飞书开放平台-发送个人&群名片消息示例

前言

本文我们基于飞书开放平台提供了服务端SDK,来教下大家如何发送个人和群名片消息

代码示例

本文我们基于飞书开平提供的go-sdk进行展示,go-sdk的github地址为:
https://github.com/larksuite/oapi-sdk-go

代码示例如下:

package main

import (
    "context"
    "fmt"
    lark "github.com/larksuite/oapi-sdk-go/v3"
    larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
    larkim "github.com/larksuite/oapi-sdk-go/v3/service/im/v1"
    "os"
)

func sendShardChatMsg(client *lark.Client) {
    msgShareChat := larkim.MessageShareChat{ChatId: "oc_3c91943a1276c5d9fe44717deb1682b7"}
    content, err := msgShareChat.String()
    if err != nil {
        fmt.Println(err)
        return
    }
    resp, err := client.Im.Message.Create(context.Background(), larkim.NewCreateMessageReqBuilder().
        ReceiveIdType(larkim.ReceiveIdTypeOpenId).
        Body(larkim.NewCreateMessageReqBodyBuilder().
            MsgType(larkim.MsgTypeShareChat).
            ReceiveId("ou_c245b0a7dff2725cfa2fb104f8b48b9d").
            Content(content).
            Build()).
        Build())

    if err != nil {
        fmt.Println(err)
        return
    }

    if !resp.Success() {
        fmt.Println(resp.Code, resp.Msg, resp.RequestId())
        return
    }

    fmt.Println(larkcore.Prettify(resp))
    fmt.Println(resp.RequestId())

}

func sendShardUserMsg(client *lark.Client) {
    msgShareUser := larkim.MessageShareUser{UserId: "ou_487f709a942d16edafe57fd6fbc4bcf5"}
    content, err := msgShareUser.String()
    if err != nil {
        fmt.Println(err)
        return
    }
    resp, err := client.Im.Message.Create(context.Background(), larkim.NewCreateMessageReqBuilder().
        ReceiveIdType(larkim.ReceiveIdTypeOpenId).
        Body(larkim.NewCreateMessageReqBodyBuilder().
            MsgType(larkim.MsgTypeShareUser).
            ReceiveId("ou_c245b0a7dff2725cfa2fb104f8b48b9d").
            Content(content).
            Build()).
        Build())

    if err != nil {
        fmt.Println(err)
        return
    }

    if !resp.Success() {
        fmt.Println(resp.Code, resp.Msg, resp.RequestId())
        return
    }

    fmt.Println(larkcore.Prettify(resp))
    fmt.Println(resp.RequestId())

}
func main() {
    // 创建 api client
    var appID, appSecret = os.Getenv("APP_ID"), os.Getenv("APP_SECRET")
    client := lark.NewClient(appID, appSecret)

    // 发送群名片消息
    sendShardChatMsg(client)

    // 发送个人名片消息
    sendShardUserMsg(client)

}

运行后,机器人就会给指定的接受者发送两个消息,消息内容如下:


image.png

配套讲解视频

https://www.bilibili.com/video/BV1w8411J7cc/?spm_id_from=333.999.0.0&vd_source=7ccc270970b6d95e716350d3f0ebff69

你可能感兴趣的:(飞书开放平台-发送个人&群名片消息示例)