前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >飞书开放平台-回复消息示例

飞书开放平台-回复消息示例

作者头像
加多
发布2023-01-12 10:50:58
1.1K0
发布2023-01-12 10:50:58
举报
文章被收录于专栏:Java编程技术Java编程技术

前言

本文我们基于飞书开放平台提供的服务端SDK,展示下如何回复一个指定的消息

代码示例

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

代码示例如下:

代码语言:javascript
复制
// Package im code generated by oapi sdk gen
/*
 * MIT License
 *
 * Copyright (c) 2022 Lark Technologies Pte. Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package main

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

func sendMsg(client *lark.Client) (string, error) {
    content := larkim.NewTextMsgBuilder().
        Text("nice to meet you").
        AtUser("ou_c245b0a7dff2725cfa2fb104f8b48b9d", "加多").
        Build()

    resp, err := client.Im.Message.Create(context.Background(), larkim.NewCreateMessageReqBuilder().
        ReceiveIdType(larkim.ReceiveIdTypeOpenId).
        Body(larkim.NewCreateMessageReqBodyBuilder().
            MsgType(larkim.MsgTypeText).
            ReceiveId("ou_c245b0a7dff2725cfa2fb104f8b48b9d").
            Content(content).
            Build()).
        Build())

    if err != nil {
        return "", err
    }

    if !resp.Success() {
        return "", errors.New(fmt.Sprintf("%d,%s,%s", resp.Code, resp.Msg, resp.RequestId()))
    }

    return *resp.Data.MessageId, nil
}

func replayMsg(client *lark.Client, msgId string) error {
    // 构建文本消息
    content := larkim.NewTextMsgBuilder().
        Text("nice to meet you too").
        Build()

    // 创建请求对象
    req := larkim.NewReplyMessageReqBuilder().
        MessageId(msgId).
        Body(larkim.NewReplyMessageReqBodyBuilder().
            Content(content).
            MsgType(larkim.MsgTypeText).
            Build()).
        Build()
    // 发起请求
    resp, err := client.Im.Message.Reply(context.Background(), req)

    // 处理错误
    if err != nil {
        return err
    }

    // 服务端错误处理
    if !resp.Success() {
        return errors.New(fmt.Sprintf("%d,%s,%s", resp.Code, resp.Msg, resp.RequestId()))

    }
    // 业务处理
    fmt.Println(larkcore.Prettify(resp))
    return nil
}

// POST /open-apis/im/v1/messages/:message_id/reply
func main() {
    // 创建client
    var appID, appSecret = os.Getenv("APP_ID"), os.Getenv("APP_SECRET")
    client := lark.NewClient(appID, appSecret)

    // 发送消息获取msgId
    msgID, err := sendMsg(client)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(msgID)

    // 回复消息
    replayMsg(client, msgID)

}

运行后,消息内容如下:

image.png

配套讲解视频

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-01-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 代码示例
  • 配套讲解视频
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档