forked from bots-go-framework/bots-fw
/
webhook_responder.go
59 lines (48 loc) · 1.66 KB
/
webhook_responder.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
package fbm
import (
"context"
"github.com/pkg/errors"
"github.com/strongo/bots-api-fbm"
"github.com/strongo/bots-framework/core"
"github.com/strongo/log"
"google.golang.org/appengine/urlfetch"
)
// webhookResponder responds to FB Messenger
type webhookResponder struct {
whc *fbmWebhookContext
}
var _ bots.WebhookResponder = (*webhookResponder)(nil)
// newFbmWebhookResponder creates new responder to FBM
func newFbmWebhookResponder(whc *fbmWebhookContext) webhookResponder {
responder := webhookResponder{whc: whc} // We need a dedicated to get rid of type assertion
whc.responder = responder
return responder
}
// SendMessage sends message to FBM
func (r webhookResponder) SendMessage(c context.Context, m bots.MessageFromBot, channel bots.BotAPISendMessageChannel) (resp bots.OnMessageSentResponse, err error) {
log.Debugf(c, "webhookResponder.SendMessage()...")
if m.Text != "" && m.FbmAttachment != nil {
err = errors.New("m.Text is empty string && m.FbmAttachment != nil")
return
}
request := fbmbotapi.Request{
NotificationType: fbmbotapi.RequestNotificationTypeNoPush,
//Recipient: fbm_api.RequestRecipient{},
Message: fbmbotapi.RequestMessage{
Text: m.Text,
Attachment: m.FbmAttachment,
},
}
if request.Recipient.ID, err = r.whc.BotChatID(); err != nil {
err = errors.WithMessage(err, "failed to call r.whc.BotChatID()")
return
} else if request.Recipient.ID == "" {
err = errors.New("Unknown recipient as r.whc.BotChatID() returned an empty string")
return
}
graphAPI := fbmbotapi.NewGraphAPI(urlfetch.Client(c), r.whc.GetBotSettings().Token)
if err = graphAPI.SendMessage(c, request); err != nil {
return
}
return
}