-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
echobot.go
56 lines (47 loc) · 1.49 KB
/
echobot.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
package main
import (
"context"
"log"
"os"
"github.com/deltachat/deltachat-rpc-client-go/deltachat"
"github.com/deltachat/deltachat-rpc-client-go/deltachat/transport"
)
func logEvent(bot *deltachat.Bot, accId deltachat.AccountId, event deltachat.Event) {
switch ev := event.(type) {
case deltachat.EventInfo:
log.Printf("INFO: %v", ev.Msg)
case deltachat.EventWarning:
log.Printf("WARNING: %v", ev.Msg)
case deltachat.EventError:
log.Printf("ERROR: %v", ev.Msg)
}
}
func main() {
trans := transport.NewIOTransport()
trans.Open()
defer trans.Close()
rpc := &deltachat.Rpc{Context: context.Background(), Transport: trans}
sysinfo, _ := rpc.GetSystemInfo()
log.Println("Running deltachat core", sysinfo["deltachat_core_version"])
bot := deltachat.NewBot(rpc)
accId := deltachat.GetAccount(rpc)
bot.On(deltachat.EventInfo{}, logEvent)
bot.On(deltachat.EventWarning{}, logEvent)
bot.On(deltachat.EventError{}, logEvent)
bot.OnNewMsg(func(bot *deltachat.Bot, accId deltachat.AccountId, msgId deltachat.MsgId) {
msg, _ := bot.Rpc.GetMessage(accId, msgId)
if msg.FromId > deltachat.ContactLastSpecial {
bot.Rpc.MiscSendTextMessage(accId, msg.ChatId, msg.Text)
}
})
if isConf, _ := bot.Rpc.IsConfigured(accId); !isConf {
log.Println("Bot not configured, configuring...")
err := bot.Configure(accId, os.Args[1], os.Args[2])
if err != nil {
log.Fatalln(err)
}
}
addr, _ := bot.Rpc.GetConfig(accId, "configured_addr")
log.Println("Listening at:", addr.Unwrap())
bot.Run()
}