飞书开放平台-发送图片&文件消息

前言

本文我们基于飞书开放平台提供的服务端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 uploadImage(client *lark.Client) {
    pdf, err := os.Open("/Users/bytedance/Downloads/封面.jpg")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer pdf.Close()

    resp, err := client.Im.Image.Create(context.Background(),
        larkim.NewCreateImageReqBuilder().
            Body(larkim.NewCreateImageReqBodyBuilder().
                ImageType(larkim.ImageTypeMessage).
                Image(pdf).
                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 uploadFile(client *lark.Client) {
    file, err := os.Open("/Users/bytedance/Downloads/open-redis.pdf")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer file.Close()

    resp, err := client.Im.File.Create(context.Background(),
        larkim.NewCreateFileReqBuilder().
            Body(larkim.NewCreateFileReqBodyBuilder().
                FileType(larkim.FileTypePdf).
                FileName("open-redis.pdf").
                File(file).
                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 sendImageMsg(client *lark.Client) {
    msgImage := larkim.MessageImage{ImageKey: "img_v2_a66c4f79-c7b5-4899-b5e3-622766c4f82g"}
    content, err := msgImage.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.MsgTypeImage).
            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 sendFileMsg(client *lark.Client) {
    msgImage := larkim.MessageFile{FileKey: "file_v2_4fa17cda-01f3-4aac-927a-7833ab482fcg"}
    content, err := msgImage.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.MsgTypeFile).
            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)

    // 发送图片消息
    uploadImage(client)
    sendImageMsg(client)

    // 发送文件消息
    uploadFile(client)
    sendFileMsg(client)
}

运行后,机器人就会给指定的接受者发送两个消息(一个图片消息,一个文件消息),消息内容如下:

飞书开放平台-发送图片&文件消息_第1张图片 image.png

配套讲解视频

单击左下角阅读原文,可看对应视频讲解

你可能感兴趣的:(飞书,java,开发语言)