init: Initial commit
This commit is contained in:
56
app/bot/conversation.go
Normal file
56
app/bot/conversation.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
"git.secnex.io/secnex/taro-bot/config"
|
||||
)
|
||||
|
||||
type Conversation struct {
|
||||
auth Auth
|
||||
}
|
||||
|
||||
func NewConversation(auth Auth) *Conversation {
|
||||
return &Conversation{
|
||||
auth: auth,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Conversation) SendMessage(message *Message) error {
|
||||
token, err := c.auth.GetToken()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
json, err := message.ToJSON()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
trafficManagerUrl := config.CONFIG.MicrosoftTeamsBotUrl
|
||||
requestUrl := fmt.Sprintf("%s/v3/conversations", trafficManagerUrl)
|
||||
req, err := http.NewRequest("POST", requestUrl, strings.NewReader(json))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
statusCode := resp.StatusCode
|
||||
masterlog.Info("Sent message via Teams Bot successfully", map[string]interface{}{"statusCode": statusCode})
|
||||
if statusCode >= 300 {
|
||||
return errors.New(string(body))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user