1
0
Fork 0
mirror of synced 2025-04-19 08:50:56 +00:00

Compare commits

..

No commits in common. "master" and "v1.3.21" have entirely different histories.

5 changed files with 52 additions and 88 deletions

View file

@ -1,7 +1,6 @@
module github.com/retailcrm/mg-transport-api-client-go/examples/telegram
go 1.21.5
toolchain go1.23.7
require (
github.com/gin-gonic/gin v1.9.1
@ -33,10 +32,10 @@ require (
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.35.0 // indirect
golang.org/x/net v0.36.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View file

@ -78,16 +78,16 @@ github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZ
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
golang.org/x/net v0.36.0 h1:vWF2fRbw4qslQsQzgFqZff+BItCvGFQqKzKIzx1rmoA=
golang.org/x/net v0.36.0/go.mod h1:bFmbeoIPfrw4sMHNhb4J9f6+tPziuGjq7Jk/38fxi1I=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=

View file

@ -592,32 +592,39 @@ func (c *MgClient) MessagesHistory(request SendHistoryMessageRequest) (MessagesR
//
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// status, err := client.AddMessageReaction(ReactionRequest{
// Channel: 305,
// _, status, err := client.AddMessageReaction(ReactionRequest{
// ChannelID: 305,
// Message: ReactionMessageReference{
// ExternalID: "uid_1",
// },
// Reaction: "😁",
// Reaction: "😁",
// })
// if err != nil {
// log.Fatalf("request error: %s (%d)", err, status)
// }
//
// log.Printf("status: %d", status)
func (c *MgClient) AddMessageReaction(request ReactionRequest) (int, error) {
var outgoing = &bytes.Buffer{}
func (c *MgClient) AddMessageReaction(request ReactionRequest) (MessageReactionResponse, int, error) {
var (
resp MessageReactionResponse
outgoing = &bytes.Buffer{}
)
_ = json.NewEncoder(outgoing).Encode(request)
data, status, err := c.PostRequest("/messages/reaction", outgoing)
if err != nil {
return status, err
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return status, NewAPIClientError(data)
return resp, status, NewAPIClientError(data)
}
return status, err
return resp, status, err
}
// DeleteMessagesReaction removes reactions to the message.
@ -628,31 +635,36 @@ func (c *MgClient) AddMessageReaction(request ReactionRequest) (int, error) {
//
// client := New("https://message-gateway.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// status, err := client.DeleteMessagesReaction(ReactionRequest{
// ChannelID: 305,
// _, status, err := client.DeleteMessagesReaction(ReactionRequest{
// ChannelID: 305,
// Message: ReactionMessageReference{
// ExternalID: "uid_1",
// },
// Reaction: "😁",
// Reaction: "😁",
// })
// if err != nil {
// log.Fatalf("request error: %s (%d)", err, status)
// }
//
// log.Printf("status: %d", status)
func (c *MgClient) DeleteMessagesReaction(request ReactionRequest) (int, error) {
func (c *MgClient) DeleteMessagesReaction(request ReactionRequest) (MessageReactionResponse, int, error) {
var resp MessageReactionResponse
outgoing, _ := json.Marshal(&request)
data, status, err := c.DeleteRequest("/messages/reaction", outgoing)
if err != nil {
return status, err
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status != http.StatusOK {
return status, NewAPIClientError(data)
return resp, status, NewAPIClientError(data)
}
return status, err
return resp, status, err
}
// UpdateMessages edits existing message. Only text messages are supported.

View file

@ -890,7 +890,7 @@ func (t *MGClientTest) Test_AddMessageReaction() {
c := t.client()
snd := ReactionRequest{
Channel: 1,
ChannelID: 1,
Message: ReactionMessageReference{
ExternalID: "external_1",
},
@ -901,43 +901,19 @@ func (t *MGClientTest) Test_AddMessageReaction() {
t.gock().
Post(t.transportURL("messages/reaction")).
Reply(http.StatusOK).
JSON(``)
JSON(MessageReactionResponse{})
status, err := c.AddMessageReaction(snd)
_, status, err := c.AddMessageReaction(snd)
t.Require().NoError(err)
t.Assert().Equal(http.StatusOK, status)
t.Assert().Empty(gock.GetUnmatchedRequests())
}
func (t *MGClientTest) Test_AddMessageReaction_error() {
c := t.client()
snd := ReactionRequest{
Channel: 1,
Message: ReactionMessageReference{
ExternalID: "external_1",
},
Reaction: "😁",
}
defer gock.Off()
t.gock().
Post(t.transportURL("messages/reaction")).
Reply(http.StatusBadRequest).
JSON(`{"errors": ["problems"]}`)
status, err := c.AddMessageReaction(snd)
t.Assert().Equal(http.StatusBadRequest, status)
t.Assert().Error(err)
t.Assert().Equal("problems", err.Error())
t.Assert().Empty(gock.GetUnmatchedRequests())
}
func (t *MGClientTest) Test_DeleteMessagesReaction() {
c := t.client()
snd := ReactionRequest{
Channel: 1,
ChannelID: 1,
Message: ReactionMessageReference{
ExternalID: "external_1",
},
@ -948,38 +924,14 @@ func (t *MGClientTest) Test_DeleteMessagesReaction() {
t.gock().
Delete(t.transportURL("messages/reaction")).
Reply(http.StatusOK).
JSON(``)
JSON(MessageReactionResponse{})
status, err := c.DeleteMessagesReaction(snd)
_, status, err := c.DeleteMessagesReaction(snd)
t.Require().NoError(err)
t.Assert().Equal(http.StatusOK, status)
t.Assert().Empty(gock.GetUnmatchedRequests())
}
func (t *MGClientTest) Test_DeleteMessagesReaction_error() {
c := t.client()
snd := ReactionRequest{
Channel: 1,
Message: ReactionMessageReference{
ExternalID: "external_1",
},
Reaction: "😁",
}
defer gock.Off()
t.gock().
Delete(t.transportURL("messages/reaction")).
Reply(http.StatusBadRequest).
JSON(`{"errors": ["problems"]}`)
status, err := c.DeleteMessagesReaction(snd)
t.Assert().Equal(http.StatusBadRequest, status)
t.Assert().Error(err)
t.Assert().Equal("problems", err.Error())
t.Assert().Empty(gock.GetUnmatchedRequests())
}
func (t *MGClientTest) Test_MarkMessageReadAndDelete() {
c := t.client()

View file

@ -358,9 +358,9 @@ type SendHistoryMessageRequest struct {
}
type ReactionRequest struct {
Channel uint64 `json:"channel"`
Message ReactionMessageReference `json:"message"`
Reaction string `json:"reaction,omitempty"`
ChannelID uint64 `json:"channel_id"`
Message ReactionMessageReference `json:"message"`
Reaction string `json:"reaction,omitempty"`
}
type ReactionMessageReference struct {
@ -403,6 +403,8 @@ type SendMessageRequestQuote struct {
// MarkMessageReadResponse type.
type MarkMessageReadResponse struct{}
type MessageReactionResponse struct{}
// MarkMessageReadRequest type.
type MarkMessageReadRequest struct {
Message MarkMessageReadRequestMessage `json:"message"`
@ -459,9 +461,8 @@ type WebhookMessageSentResponse struct {
// MessageSentError type.
type MessageSentError struct {
Code ErrorType `json:"code"`
Message string `json:"message"`
ExternalCode string `json:"external_code,omitempty"`
Code ErrorType `json:"code"`
Message string `json:"message"`
}
// MessageWebhookData request data.