forked from deltachat-bot/invitebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup-editor-bot.go
224 lines (204 loc) · 6.76 KB
/
group-editor-bot.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"math/rand"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/cavaliergopher/grab/v3"
"github.com/deltachat-bot/deltabot-cli-go/botcli"
"github.com/deltachat/deltachat-rpc-client-go/deltachat"
"github.com/deltachat/deltachat-rpc-client-go/deltachat/option"
qrcode "github.com/skip2/go-qrcode"
"github.com/spf13/cobra"
)
var cli = botcli.New("group-editor-bot")
func onBotInit(cli *botcli.BotCli, bot *deltachat.Bot, cmd *cobra.Command, args []string) {
bot.OnNewMsg(onNewMsg)
accounts, err := bot.Rpc.GetAllAccountIds()
if err != nil {
cli.Logger.Error(err)
}
for _, accId := range accounts {
name, err := bot.Rpc.GetConfig(accId, "displayname")
if err != nil {
cli.Logger.Error(err)
}
if name.UnwrapOr("") == "" {
err = bot.Rpc.SetConfig(accId, "displayname", option.Some("Group Editor Bot"))
if err != nil {
cli.Logger.Error(err)
}
status := "I am a bot that helps managing editors in groups, send me /help for more info"
err = bot.Rpc.SetConfig(accId, "selfstatus", option.Some(status))
if err != nil {
cli.Logger.Error(err)
}
err = bot.Rpc.SetConfig(accId, "delete_server_after", option.Some("1"))
if err != nil {
cli.Logger.Error(err)
}
}
}
}
func onNewMsg(bot *deltachat.Bot, accId deltachat.AccountId, msgId deltachat.MsgId) {
logger := cli.GetLogger(accId).With("msg", msgId)
selfAddr, err := bot.Rpc.GetConfig(accId, "addr")
msg, err := bot.Rpc.GetMessage(accId, msgId)
if err != nil {
logger.Error(err)
return
}
if msg.SystemMessageType == deltachat.SysmsgMemberAddedToGroup {
resendPads(bot.Rpc, accId, msg.ChatId)
}
if msg.SystemMessageType == deltachat.SysmsgMemberRemovedFromGroup {
if strings.Contains(msg.Text, "Member Me ("+*selfAddr.Value+") removed by ") {
bot.Rpc.DeleteChat(accId, msg.ChatId)
}
}
if !msg.IsBot && !msg.IsInfo && msg.FromId > deltachat.ContactLastSpecial {
chat, err := bot.Rpc.GetBasicChatInfo(accId, msg.ChatId)
if err != nil {
logger.Error(err)
return
}
if chat.ChatType == deltachat.ChatSingle || strings.HasPrefix(msg.Text, "/") {
err = bot.Rpc.MarkseenMsgs(accId, []deltachat.MsgId{msg.Id})
if err != nil {
logger.Error(err)
}
}
args := strings.Split(msg.Text, " ")
switch args[0] {
case "/invite":
if chat.ChatType == deltachat.ChatGroup {
sendInviteQr(bot.Rpc, accId, msg.ChatId)
} else {
text := "The /invite command can only be used in groups, send /help for more info"
_, err := bot.Rpc.SendMsg(accId, msg.ChatId, deltachat.MsgData{Text: text})
if err != nil {
logger.Error(err)
}
}
case "/editor":
sendPad(bot.Rpc, accId, msg.ChatId, msg.Text)
return
case "/help":
sendHelp(bot.Rpc, accId, msg.ChatId)
default:
if chat.ChatType == deltachat.ChatSingle {
sendHelp(bot.Rpc, accId, msg.ChatId)
}
}
}
if msg.Sender.Address != selfAddr.Unwrap() || msg.WebxdcInfo == nil {
err = bot.Rpc.DeleteMessages(accId, []deltachat.MsgId{msg.Id})
if err != nil {
logger.Error(err)
} else {
println("Deleted message " + strconv.FormatUint(uint64(msg.Id), 10))
}
}
}
func sendPad(rpc *deltachat.Rpc, accId deltachat.AccountId, chatId deltachat.ChatId, command string) {
HomeDir, err := os.UserHomeDir()
editor_path := filepath.Join(HomeDir, ".config", "group-editor-bot", "jagtalon-realtime-editor-v4.0.4.xdc")
var description string
if len(command) > 7 {
description = command[8:] // bot adds text after /editor as description to the editor.xdc message
} else {
description = ""
}
msgID, err := rpc.SendMsg(accId, chatId, deltachat.MsgData{Text: description, File: editor_path})
if err != nil {
cli.GetLogger(accId).With("chat", chatId).Error(err)
}
cli.Logger.Info("Sent editor message " + string(msgID))
}
func resendPads(rpc *deltachat.Rpc, accId deltachat.AccountId, chatId deltachat.ChatId) {
var toResend []deltachat.MsgId
selfAddr, err := rpc.GetConfig(accId, "addr")
if err == nil {
msgIds, _ := rpc.GetMessageIds(accId, chatId, false, false)
var msgIdStrings []string
for i := range msgIds {
msgIdStrings = append(msgIdStrings, strconv.FormatUint(uint64(msgIds[i]), 10))
}
// println("In this chat I know the messages: " + strings.Join(msgIdStrings, ","))
for _, id := range msgIds {
msg, _ := rpc.GetMessage(accId, id)
senderaddress := msg.Sender.Address
// println(strconv.FormatUint(uint64(msg.Id), 10) + senderaddress + selfAddr.Unwrap())
if senderaddress == selfAddr.Unwrap() && msg.WebxdcInfo != nil {
toResend = append(toResend, id)
}
}
if toResend != nil {
err := rpc.ResendMessages(accId, toResend)
for err != nil {
var msgIdsStrings []string
for i := range toResend {
msgIdsStrings = append(msgIdsStrings, strconv.FormatUint(uint64(toResend[i]), 10))
}
cli.Logger.Error("Resending messages " + strings.Join(msgIdsStrings, ",") + " failed with error: '" + err.Error() + "'. Retrying.")
r := rand.Intn(10)
time.Sleep(time.Duration(r) * time.Second)
err = rpc.ResendMessages(accId, toResend)
}
}
}
}
func sendHelp(rpc *deltachat.Rpc, accId deltachat.AccountId, chatId deltachat.ChatId) {
text := "I am a bot that manages editors in groups.\n\n"
text += "To create a new shared editor for the group, you can write:\n\n"
text += "/editor Shopping List for Friday's Example Party\n\n"
text += "I will send an editor to the group, which anyone can edit; and if new members are added, they will see it, too."
msgId, err := rpc.SendMsg(accId, chatId, deltachat.MsgData{Text: text})
if err != nil {
cli.GetLogger(accId).With("chat", chatId).Error(err)
}
time.Sleep(10 * time.Second) // sleep for 10 seconds, so the message has a chance to be sent
err = rpc.DeleteMessages(accId, []deltachat.MsgId{msgId})
if err != nil {
cli.Logger.Error(err)
}
}
func sendInviteQr(rpc *deltachat.Rpc, accId deltachat.AccountId, chatId deltachat.ChatId) {
logger := cli.GetLogger(accId).With("chat", chatId)
qrdata, _, err := rpc.GetChatSecurejoinQrCodeSvg(accId, option.Some(chatId))
if err != nil {
logger.Error(err)
return
}
dir, err := os.MkdirTemp("", "")
if err != nil {
logger.Error(err)
return
}
defer os.RemoveAll(dir)
path := filepath.Join(dir, "qr.png")
err = qrcode.WriteFile(qrdata, qrcode.Medium, 256, path)
if err != nil {
logger.Error(err)
return
}
_, err = rpc.SendMsg(accId, chatId, deltachat.MsgData{Text: qrdata, File: path})
if err != nil {
logger.Error(err)
}
}
func main() {
cli.OnBotInit(onBotInit)
HomeDir, err := os.UserHomeDir()
DownloadDir := filepath.Join(HomeDir, ".config", "group-editor-bot")
resp, err := grab.Get(DownloadDir, "https://apps.testrun.org/jagtalon-realtime-editor-v4.0.4.xdc")
if err != nil {
cli.Logger.Error(err)
}
cli.Logger.Info("Download saved to ", resp.Filename)
if err := cli.Start(); err != nil {
cli.Logger.Error(err)
}
}