美文网首页
飞书开放平台-回复消息示例

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

作者: 阿里加多 | 来源:发表于2023-01-10 21:00 被阅读0次

前言

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

代码示例

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

代码示例如下:

// 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

相关文章

  • 6月 20日

    一、问题汇总 1.消息回复不及时!微信讨论问题,飞书加急信息~微信问问题要记得艾特我和小宇,飞书加急信息要收到回复...

  • 飞书开放平台官方golang-sdk

    旨在让开发者便捷的调用飞书开放API、处理订阅的消息事件、处理服务端推送的卡片行为。 安装 API Client ...

  • CTWing电信物联网平台HTTP消息推送

    CTWing电信物联网平台HTTP消息推送 HTTP消息推送 订阅推送消息格式 示例 订阅管理

  • 侯海锋 从零开始微信公众号 如何设置消息自动回复

    设置方法:通过在微信公众平台->功能->自动回复->消息自动回复,可设置的文字/语音/图片/视频为用户消息回复。 ...

  • 互联网设计小报—第7期

    ? 一个工具推荐 飞书多维表格beta版已开放使用 链接地址:飞书多维表格[https://bitable.fei...

  • Android极光推送(接入小米厂商)

    小米开放平台[https://dev.mi.com/console/] 登录小米开放平台账号,点击应用服务-消息推...

  • OkHttp3的用法Demo

    maven架包导入 java代码 调用请求代码示例:这是调用访问钉钉的开发开放平台

  • 回复消息

    老公去拉萨的第十天,终于找了件事吵了一架…… 从高中到小学,就是想日子过得悠闲点,可现在咋感觉这日子是越活...

  • 消息回复

    等你的消息 就像去期待一场南方的初雪 姗姗来迟,煎熬漫长 你回我消息 就像孩子口袋里的糖纸 皱巴巴一句,已然让我贪...

  • 飞书消息推送怎么换行?输入\n发送不成功怎么办?

    公司用飞书的话,我们经常需要向飞书的一些群里推送消息。有些消息比较长,就涉及到格式问题,否则发到群里一乱堆,阅读起...

网友评论

      本文标题:飞书开放平台-回复消息示例

      本文链接:https://www.haomeiwen.com/subject/hpkpcdtx.html