-
Notifications
You must be signed in to change notification settings - Fork 244
/
messenger_auto_message.go
75 lines (62 loc) · 1.81 KB
/
messenger_auto_message.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
package protocol
import (
"context"
"fmt"
"time"
"go.uber.org/zap"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/signal"
)
// autoMessageInterval is how often we should send a message
const autoMessageInterval = 120 * time.Second
const autoMessageChatID = "status-bot"
func (m *Messenger) AutoMessageEnabled() (bool, error) {
return m.settings.AutoMessageEnabled()
}
func (m *Messenger) startAutoMessageLoop() error {
enabled, err := m.AutoMessageEnabled()
if err != nil {
m.logger.Error("[auto message] failed to start auto message loop", zap.Error(err))
return err
}
if !enabled {
return nil
}
m.logger.Info("[auto message] starting auto message loop")
ticker := time.NewTicker(autoMessageInterval)
count := 0
go func() {
for {
select {
case <-ticker.C:
count++
timestamp := time.Now().Format(time.RFC3339)
msg := common.NewMessage()
msg.Text = fmt.Sprintf("%d\n%s", count, timestamp)
msg.ChatId = autoMessageChatID
msg.LocalChatID = autoMessageChatID
msg.ContentType = protobuf.ChatMessage_TEXT_PLAIN
resp, err := m.SendChatMessage(context.Background(), msg)
if err != nil {
m.logger.Error("[auto message] failed to send message", zap.Error(err))
continue
}
signal.SendNewMessages(resp)
err = m.UpdateMessageOutgoingStatus(msg.ID, common.OutgoingStatusDelivered)
if err != nil {
m.logger.Error("[auto message] failed to mark message as delivered", zap.Error(err))
continue
}
//send signal to client that message status updated
if m.config.messengerSignalsHandler != nil {
m.config.messengerSignalsHandler.MessageDelivered(autoMessageChatID, msg.ID)
}
case <-m.quit:
ticker.Stop()
return
}
}
}()
return nil
}