-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcNewIntegration.go
47 lines (43 loc) · 1.09 KB
/
funcNewIntegration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package ihcph
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/sp0x/ihcph/funcBotIntegration"
"github.com/sp0x/ihcph/telegram"
"net/http"
)
type NewBotRequest struct {
Token string `json:"token"`
}
type NewBotRequestResponse struct {
Id string `json:"id"`
}
func NewBotIntegration(w http.ResponseWriter, r *http.Request) {
_, err := funcBotIntegration.Initialize()
if err != nil {
http.Error(w, fmt.Sprintf("couldn't initialize: %v", err), 500)
return
}
newBot := NewBotRequest{}
err = json.NewDecoder(r.Body).Decode(&newBot)
if err != nil {
http.Error(w, "Couldn't decode body", 400)
return
}
newIntegration := &telegram.Integration{
Token: newBot.Token,
}
bot := telegram.NewBotInterface()
err = bot.StoreNewIntegration(newIntegration)
if err != nil {
log.Errorf("error while creating integration: %v", err)
http.Error(w, "Couldn't save integration", 500)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(NewBotRequestResponse{
newIntegration.Id,
})
}