From d502a38b1abb536db1c03445f855c916bc3c6fb8 Mon Sep 17 00:00:00 2001 From: Yader Velasquez Date: Tue, 20 Feb 2024 17:34:35 -0600 Subject: [PATCH] Get oauth token handler --- teams/helpers.go | 29 +++++++++++++++++++++++ teams/routes.go | 61 +++++++++++++++++++++++++++--------------------- 2 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 teams/helpers.go diff --git a/teams/helpers.go b/teams/helpers.go new file mode 100644 index 0000000..c8842b4 --- /dev/null +++ b/teams/helpers.go @@ -0,0 +1,29 @@ +package teams + +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"` +} diff --git a/teams/routes.go b/teams/routes.go index 76fdfd6..94466ac 100644 --- a/teams/routes.go +++ b/teams/routes.go @@ -5,8 +5,10 @@ import ( "encoding/json" "fmt" "net/http" + "net/url" "github.com/labstack/echo/v4" + "netlandish.com/x/gobwebs/server" ) type Service struct { @@ -15,40 +17,47 @@ type Service struct { } func (s *Service) RegisterRoutes() { - s.eg.POST("/integration", s.SendMsg).Name = s.RouteName("integration") - s.eg.GET("/integration", s.SendMsg).Name = s.RouteName("integration") + s.eg.POST("/send", s.SendMsg).Name = s.RouteName("send") + s.eg.GET("/send", s.SendMsg).Name = s.RouteName("send") + s.eg.GET("/connect", s.Connect).Name = s.RouteName("connect") } -type TeamSubItem struct { - ID string `json:"id"` - Name string `json:"name"` -} +func (s *Service) Connect(c echo.Context) error { + gctx := c.(*server.Context) + appID, ok := gctx.Server.Config.File.Get("teams", "app-id") + if !ok { + return fmt.Errorf("teams app-id not found") + } + clientSecret, ok := gctx.Server.Config.File.Get("teams", "client-secret") + if !ok { + return fmt.Errorf("teams client-secret not found") + } -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"` -} + authURL, ok := gctx.Server.Config.File.Get("teams", "auth-url") + if !ok { + return fmt.Errorf("teams auth-url not found") + } -type Member struct { -} + data := url.Values{} + data.Set("grant_type", "client_credentials") + data.Set("client_id", appID) + data.Set("client_secret", clientSecret) + data.Set("scope", "https://api.botframework.com/.default") -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"` + req, err := http.NewRequest(http.MethodPost, authURL, bytes.NewBufferString(data.Encode())) + if err != nil { + return err + } + client := http.Client{} + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + return c.JSON(http.StatusOK, "OK") } 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 { -- 2.45.2