2021-11-29 13:11:55 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2024-09-26 22:37:32 +00:00
|
|
|
gocommon "github.com/status-im/status-go/common"
|
2021-11-29 13:11:55 +00:00
|
|
|
"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
|
|
|
|
|
2022-08-31 14:41:58 +00:00
|
|
|
const autoMessageChatID = "status-bot"
|
2021-11-29 13:11:55 +00:00
|
|
|
|
|
|
|
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() {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2021-11-29 13:11:55 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
count++
|
|
|
|
timestamp := time.Now().Format(time.RFC3339)
|
|
|
|
|
2023-08-18 11:39:59 +00:00
|
|
|
msg := common.NewMessage()
|
2021-11-29 13:11:55 +00:00
|
|
|
msg.Text = fmt.Sprintf("%d\n%s", count, timestamp)
|
2022-08-31 14:41:58 +00:00
|
|
|
msg.ChatId = autoMessageChatID
|
|
|
|
msg.LocalChatID = autoMessageChatID
|
2021-11-29 13:11:55 +00:00
|
|
|
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 {
|
2022-08-31 14:41:58 +00:00
|
|
|
m.config.messengerSignalsHandler.MessageDelivered(autoMessageChatID, msg.ID)
|
2021-11-29 13:11:55 +00:00
|
|
|
}
|
|
|
|
case <-m.quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|