forked from bigbluebutton-bot/bigbluebutton-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_GroupChatMsg.go
More file actions
77 lines (61 loc) · 1.92 KB
/
event_GroupChatMsg.go
File metadata and controls
77 lines (61 loc) · 1.92 KB
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
package bot
import (
"errors"
"reflect"
"time"
ddp "github.com/gopackage/ddp"
"github.com/benpate/convert"
bbb "github.com/bigbluebutton-bot/bigbluebutton-bot/bbb"
)
type groupChatMsgListener func(msg bbb.Message)
// OnGroupChatMsg in order to receive GroupChatMsg changes.
func (c *Client) OnGroupChatMsg(listener groupChatMsgListener) error {
if c.events["OnGroupChatMsg"] == nil {
if err := c.ddpSubscribe(bbb.GroupChatSub, nil); err != nil {
return err
}
if err := c.ddpSubscribe(bbb.GroupChatMsgSub, c.updateGroupChatMsg); err != nil {
return err
}
}
c.events["OnGroupChatMsg"] = append(c.events["OnGroupChatMsg"], listener)
return nil
}
// informs all listeners with the new infos.
func (c *Client) updateGroupChatMsg(collection string, operation string, id string, doc ddp.Update) {
if doc == nil || doc["id"] == nil {
return
}
msg := bbb.ConvertInToMessage(doc)
// Inform all listeners
for _, event := range c.events["OnGroupChatMsg"] {
// call event(infos)
f := reflect.TypeOf(event)
if f.Kind() == reflect.Func { //is function
if f.NumIn() == 1 && f.NumOut() == 0 { //inbound parameters == 1, outbound parameters == 0
if f.In(0).Kind() == reflect.Struct { //parameter 0 is of type string (string){ //parameter 3 is of type struct (ddp.Update)
go reflect.ValueOf(event).Call([]reflect.Value{reflect.ValueOf(msg)})
}
}
}
}
}
func (c *Client) SendChatMsg(message string, chatId string) error {
now := time.Now()
timestemp := convert.String(now.UnixNano())
messageSend := bbb.MessageSend{
ID: c.InternalUserID + timestemp[:len(timestemp)-(len(timestemp)-13)],
Sender: bbb.MessageSendSender{
ID: c.InternalUserID,
Name: "",
Role: "",
},
ChatEmphasizedText: true,
Message: message,
}
_, err := c.ddpCall(bbb.SendGroupChatMsgCall, chatId, messageSend)
if err != nil {
return errors.New("could not send message: " + err.Error())
}
return nil
}