From a091ee8a3dd8cafc9d81932f7584b7188cd2e613 Mon Sep 17 00:00:00 2001 From: Yader Velasquez Date: Mon, 19 Feb 2024 19:37:30 -0600 Subject: [PATCH] Send a simple request to Teams References: https://todo.code.netlandish.com/~netlandish/links/25 --- cmd/links/main.go | 4 ++ teams/routes.go | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 teams/routes.go diff --git a/cmd/links/main.go b/cmd/links/main.go index 6c926ee..1fe78e2 100644 --- a/cmd/links/main.go +++ b/cmd/links/main.go @@ -22,6 +22,7 @@ import ( "links/models" "links/short" "links/slack" + "links/teams" "github.com/alexedwards/scs/postgresstore" "github.com/alexedwards/scs/v2" @@ -267,6 +268,8 @@ func run() error { analytics.NewService(analyticsService) billingService := e.Group("/billing") billing.NewService(billingService) + teamsService := e.Group("/teams") + teams.NewService(teamsService) oauthConfig := oauth2.ServiceConfig{ Helper: &core.OAuth2Helper{}, @@ -300,6 +303,7 @@ func run() error { e.Reverse("mattermost:command_search"), e.Reverse("mattermost:command_short"), e.Reverse("mattermost:command_connect"), + e.Reverse("teams:integration"), e.Reverse("billing:stripe_webhook"), e.Reverse("ses-feedback:endpoint"), ) diff --git a/teams/routes.go b/teams/routes.go new file mode 100644 index 0000000..76fdfd6 --- /dev/null +++ b/teams/routes.go @@ -0,0 +1,104 @@ +package teams + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + + "github.com/labstack/echo/v4" +) + +type Service struct { + name string + eg *echo.Group +} + +func (s *Service) RegisterRoutes() { + s.eg.POST("/integration", s.SendMsg).Name = s.RouteName("integration") + s.eg.GET("/integration", s.SendMsg).Name = s.RouteName("integration") +} + +type TeamSubItem struct { + ID string `json:"id"` + Name string `json:"name"` +} + +type TeamResponse struct { + Type string `json:"type"` + From TeamSubItem `json:"from"` + Conversation TeamSubItem `json:"conversation"` + Recipient TeamSubItem `json:"recipient"` + Text string `json:"text"` + ReplyToID string `json:"replyToId"` +} + +type Member struct { +} + +type TeamRequest struct { + ID string `json:"id"` + Type string `json:"type"` + ChannelID string `json:"channelId"` + ServiceURL string `json:"serviceURL"` + From TeamSubItem `json:"from"` + Conversation TeamSubItem `json:"conversation"` + Recipient TeamSubItem `json:"recipient"` + MembersAdded []TeamSubItem `json:"membersAdded"` +} + +func (s *Service) SendMsg(c echo.Context) error { + fmt.Println("Going here") + teamRequest := &TeamRequest{} + err := json.NewDecoder(c.Request().Body).Decode(&teamRequest) + if err != nil { + return err + } + + testResp := &TeamResponse{ + Type: "message", + From: TeamSubItem{ + ID: "123455", + Name: "Bot name", + }, + Conversation: TeamSubItem{ + ID: "123455", + Name: "Conversation name", + }, + Recipient: TeamSubItem{ + ID: "123455", + Name: "Recipient name", + }, + Text: "This is a test", + ReplyToID: "bf3cc9a2f5de", + } + + client := &http.Client{} + requestBody := new(bytes.Buffer) + err = json.NewEncoder(requestBody).Encode(testResp) + if err != nil { + return err + } + url := fmt.Sprintf("%s/v3/conversations/%s/activities", teamRequest.ServiceURL, teamRequest.ID) + request, err := http.NewRequest(http.MethodPost, url, requestBody) + if err != nil { + return err + } + _, err = client.Do(request) + if err != nil { + return err + } + return c.JSON(http.StatusOK, "OK") +} + +// RouteName ... +func (s *Service) RouteName(value string) string { + return fmt.Sprintf("%s:%s", s.name, value) +} + +// NewService return service +func NewService(eg *echo.Group) *Service { + service := &Service{name: "teams", eg: eg} + service.RegisterRoutes() + return service +} -- 2.45.2