~netlandish/links

d502a38b1abb536db1c03445f855c916bc3c6fb8 — Yader Velasquez 9 months ago a091ee8 issue-25
Get oauth token handler
2 files changed, 64 insertions(+), 26 deletions(-)

A teams/helpers.go
M teams/routes.go
A teams/helpers.go => teams/helpers.go +29 -0
@@ 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"`
}

M teams/routes.go => teams/routes.go +35 -26
@@ 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 {