~netlandish/links

a091ee8a3dd8cafc9d81932f7584b7188cd2e613 — Yader Velasquez 10 months ago 2b8c580
Send a simple request to Teams

References: https://todo.code.netlandish.com/~netlandish/links/25
2 files changed, 108 insertions(+), 0 deletions(-)

M cmd/links/main.go
A teams/routes.go
M cmd/links/main.go => cmd/links/main.go +4 -0
@@ 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"),
	)

A teams/routes.go => teams/routes.go +104 -0
@@ 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
}