Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add formdata post api #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions openapi/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type MessageAPI interface {
Message(ctx context.Context, channelID string, messageID string) (*dto.Message, error)
Messages(ctx context.Context, channelID string, pager *dto.MessagesPager) ([]*dto.Message, error)
PostMessage(ctx context.Context, channelID string, msg *dto.MessageToCreate) (*dto.Message, error)
PostMessageByFormData(ctx context.Context, channelID string, imgData []byte, msg map[string]string) (*dto.Message, error)
PatchMessage(ctx context.Context,
channelID string, messageID string, msg *dto.MessageToCreate) (*dto.Message, error)
RetractMessage(ctx context.Context, channelID, msgID string, options ...RetractMessageOption) error
Expand Down Expand Up @@ -157,6 +158,8 @@ type DirectMessageAPI interface {
CreateDirectMessage(ctx context.Context, dm *dto.DirectMessageToCreate) (*dto.DirectMessage, error)
// PostDirectMessage 在私信频道内发消息
PostDirectMessage(ctx context.Context, dm *dto.DirectMessage, msg *dto.MessageToCreate) (*dto.Message, error)
// PostDirectMessageByFormData 在私信频道内发图片消息
PostDirectMessageByFormData(ctx context.Context, dm *dto.DirectMessage, imgData []byte, msg map[string]string) (*dto.Message, error)
// RetractDMMessage 撤回私信频道消息
RetractDMMessage(ctx context.Context, guildID, msgID string, options ...RetractMessageOption) error
// PostDMSettingGuide 发送私信设置引导, jumpGuildID为设置引导要跳转的频道ID
Expand Down
17 changes: 17 additions & 0 deletions openapi/v1/direct_message.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package v1

import (
"bytes"
"context"
"time"

"github.com/tencent-connect/botgo/dto"
"github.com/tencent-connect/botgo/openapi"
Expand Down Expand Up @@ -33,6 +35,21 @@ func (o *openAPI) PostDirectMessage(ctx context.Context,
return resp.Result().(*dto.Message), nil
}

// PostDirectMessage 在私信频道内发图片消息
func (o *openAPI) PostDirectMessageByFormData(ctx context.Context,
dm *dto.DirectMessage, imgData []byte, msg map[string]string) (*dto.Message, error) {
resp, err := o.request(ctx).
SetResult(dto.Message{}).
SetPathParam("guild_id", dm.GuildID).
SetFileReader("file_image", time.Now().String(), bytes.NewReader(imgData)).
SetFormData(msg).
Post(o.getURL(dmsURI))
if err != nil {
return nil, err
}
return resp.Result().(*dto.Message), nil
}

// RetractDMMessage 撤回私信消息
func (o *openAPI) RetractDMMessage(ctx context.Context,
guildID, msgID string, options ...openapi.RetractMessageOption) error {
Expand Down
20 changes: 19 additions & 1 deletion openapi/v1/message.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package v1

import (
"bytes"
"context"
"encoding/json"
"fmt"
"time"

"github.com/tidwall/gjson"

"github.com/tencent-connect/botgo/dto"
"github.com/tencent-connect/botgo/errs"
"github.com/tencent-connect/botgo/openapi"
"github.com/tidwall/gjson"
)

// Message 拉取单条消息
Expand Down Expand Up @@ -68,6 +71,21 @@ func (o *openAPI) PostMessage(ctx context.Context, channelID string, msg *dto.Me
return resp.Result().(*dto.Message), nil
}

// PostMessageByFormData 发图片消息
func (o *openAPI) PostMessageByFormData(ctx context.Context, channelID string, imgData []byte, msg map[string]string) (*dto.Message, error) {
resp, err := o.request(ctx).
SetResult(dto.Message{}).
SetPathParam("channel_id", channelID).
SetFileReader("file_image", time.Now().String(), bytes.NewReader(imgData)).
SetFormData(msg).
Post(o.getURL(messagesURI))
if err != nil {
return nil, err
}

return resp.Result().(*dto.Message), nil
}

// PatchMessage 编辑消息
func (o *openAPI) PatchMessage(ctx context.Context,
channelID string, messageID string, msg *dto.MessageToCreate) (*dto.Message, error) {
Expand Down