-
Notifications
You must be signed in to change notification settings - Fork 384
/
context.go
62 lines (55 loc) · 2.21 KB
/
context.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
package bertybot
import (
"context"
"fmt"
"github.com/gogo/protobuf/proto"
"go.uber.org/zap"
"berty.tech/berty/v2/go/pkg/messengertypes"
)
// Context is the main argument passed to handlers.
type Context struct {
// common
HandlerType HandlerType
EventPayload proto.Message `json:"-"` // content of the payload is already available in the parsed payloads
EventType messengertypes.StreamEvent_Type
Context context.Context
Client messengertypes.MessengerServiceClient
Logger *zap.Logger
IsReplay bool // whether the event is a replayed or a fresh event
IsMine bool // whether the bot is the author
IsAck bool // whether the event is an ack
IsNew bool // whether the event is new or an entity update
// parsed payloads, depending on the context
Contact *messengertypes.Contact `json:"Contact,omitempty"`
Conversation *messengertypes.Conversation `json:"Conversation,omitempty"`
Interaction *messengertypes.Interaction `json:"Interaction,omitempty"`
Member *messengertypes.Member `json:"Member,omitempty"`
Account *messengertypes.Account `json:"Account,omitempty"`
Device *messengertypes.Device `json:"Device,omitempty"`
ConversationPK string `json:"ConversationPK,omitempty"`
UserMessage string `json:"UserMessage,omitempty"`
CommandArgs []string
// internal
initialized bool
}
// ReplyString sends a text message on the conversation related to the context.
// The conversation can be 1-1 or multi-member.
func (ctx *Context) ReplyString(text string) error {
if ctx.ConversationPK == "" {
return fmt.Errorf("unknown conversation PK, cannot reply")
}
// FIXME: support group conversation
userMessage, err := proto.Marshal(&messengertypes.AppMessage_UserMessage{Body: text})
if err != nil {
return fmt.Errorf("marshal user message failed: %w", err)
}
_, err = ctx.Client.Interact(ctx.Context, &messengertypes.Interact_Request{
Type: messengertypes.AppMessage_TypeUserMessage,
Payload: userMessage,
ConversationPublicKey: ctx.ConversationPK,
})
if err != nil {
return fmt.Errorf("interact failed: %w", err)
}
return nil
}