前言
本文我们基于飞书开放平台提供的服务端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