Skip to content

Commit

Permalink
feat: Add broker payload
Browse files Browse the repository at this point in the history
  • Loading branch information
imulab committed Oct 25, 2023
1 parent 117eb11 commit d007924
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/eclipse/paho.golang v0.12.0
github.com/google/uuid v1.3.1
github.com/rs/zerolog v1.31.0
github.com/samber/lo v1.38.1
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/eclipse/paho.golang v0.12.0 h1:EXQFJbJklDnUqW6lyAknMWRhM2NgpHxwrrL8ri
github.com/eclipse/paho.golang v0.12.0/go.mod h1:TSDCUivu9JnoR9Hl+H7sQMcHkejWH2/xKK1NJGtLbIE=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down
38 changes: 25 additions & 13 deletions route/biz.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import (
"absurdlab.io/WeTriage/internal/httpx"
"absurdlab.io/WeTriage/topic"
"context"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"github.com/eclipse/paho.golang/autopaho"
"github.com/eclipse/paho.golang/paho"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/samber/lo"
"net/http"
"sort"
"strings"
"time"
)

func NewBusinessDataHandler(
Expand Down Expand Up @@ -128,19 +131,28 @@ func (h *BusinessDataHandler) triageMessage(messageBytes []byte) (topic.Topic, e
}

func (h *BusinessDataHandler) publishMessage(ctx context.Context, message topic.Topic) error {
if _, err := h.mqtt.Publish(ctx, &paho.Publish{
QoS: 2,
Topic: fmt.Sprintf("T/WeTriage/%s", message.Name()),
Properties: &paho.PublishProperties{
ContentType: "application/json",
ResponseTopic: "",
PayloadFormat: nil,
MessageExpiry: nil,
SubscriptionIdentifier: nil,
TopicAlias: nil,
User: nil,
},
Payload: nil,
var payload = struct {
Id string `json:"id"`
CreatedAt int64 `json:"created_at"`
Topic string `json:"topic"`
Content interface{} `json:"content"`
}{
Id: uuid.NewString(),
CreatedAt: time.Now().Unix(),
Topic: message.Name(),
Content: message,
}

payloadBytes, err := json.Marshal(payload)
if err != nil {
return err
}

if _, err = h.mqtt.Publish(ctx, &paho.Publish{
QoS: 2,
Topic: fmt.Sprintf("T/WeTriage/%s", message.Name()),
Properties: &paho.PublishProperties{ContentType: "application/json"},
Payload: payloadBytes,
}); err != nil {
h.logger.Debug().Err(err).Msg("Failed to publish message to mqtt broker")
return err
Expand Down
3 changes: 3 additions & 0 deletions topic/suite_ticket_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var (
_ TriageStrategy = (*suiteTicketInfoTriageStrategy)(nil)
)

// SuiteTicketInfo 推送suite_ticket. InfoType is always suite_ticket.
//
// https://developer.work.weixin.qq.com/document/path/97173
type SuiteTicketInfo struct {
SuiteId string `xml:"SuiteId" json:"suite_id"`
InfoType string `xml:"InfoType" json:"info_type"`
Expand Down

0 comments on commit d007924

Please sign in to comment.