/
sendLineCustomQuestionHandlerBuilder.go
101 lines (85 loc) · 2.57 KB
/
sendLineCustomQuestionHandlerBuilder.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package http
import (
"bytes"
"fmt"
"net/http"
"text/template"
"github.com/VagabondDataNinjas/gizlinebot/domain"
"github.com/pkg/errors"
"github.com/labstack/echo"
"github.com/line/line-bot-sdk-go/linebot"
log "github.com/sirupsen/logrus"
"github.com/VagabondDataNinjas/gizlinebot/storage"
)
type SendLineCustomQuestionRequest struct {
QuestionId string `json:"question_id"`
Text string `json:"text"`
ReplyText string `json:"reply_text"`
}
func SendLineCustomQuestionHandlerBuilder(s *storage.Sql, lineBot *linebot.Client) func(c echo.Context) error {
return func(c echo.Context) error {
pyld := new(SendLineCustomQuestionRequest)
if err := c.Bind(pyld); err != nil {
return err
}
// add the message to the storage
if err := s.AddCustomQuestion(pyld.QuestionId, pyld.Text, pyld.ReplyText); err != nil {
return errors.Wrap(err, "Failed to add custom question")
}
// fetch all users from DB
profiles, err := s.GetAllActiveUserProfiles()
if err != nil {
return errors.Wrap(err, "Failed to fetch user profiles")
}
warnings := make([]string, 0)
for _, profile := range profiles {
msgStr, err := templateCustomQuestion(pyld.Text, profile, s)
if err != nil {
warn := fmt.Sprintf("Got error for custom question template for userId \"%s\": %s", profile.UserId, err)
log.Error(warn)
warnings = append(warnings, warn)
continue
}
lineMsg := linebot.NewTextMessage(msgStr)
logfields := log.Fields{
"DisplayName": profile.DisplayName,
"UserId": profile.UserId,
"QuestionId": pyld.QuestionId,
}
log.WithFields(logfields).Info("Sending custom question to line")
if _, err := lineBot.PushMessage(profile.UserId, lineMsg).Do(); err != nil {
warn := fmt.Sprintf("Got error when seding msg to %s: %s", profile.UserId, err)
log.WithFields(logfields).Error(warn)
warnings = append(warnings, warn)
continue
}
}
return c.JSON(http.StatusOK, map[string]interface{}{
"status": "success", "warnings": warnings,
})
}
}
func templateCustomQuestion(msgTpl string, userProfile domain.UserProfile, s *storage.Sql) (msg string, err error) {
loc, err := s.FindUserLocation(userProfile.UserId)
if err != nil {
return "", err
}
tpl, err := template.New("customQuestion").Parse(msgTpl)
if err != nil {
return "", err
}
type TplVars struct {
Location string
DisplayName string
}
buf := new(bytes.Buffer)
tplVars := TplVars{
Location: loc.NameThai,
DisplayName: userProfile.DisplayName,
}
err = tpl.Execute(buf, tplVars)
if err != nil {
return "", err
}
return buf.String(), nil
}