Skip to content

Commit ed88d0e

Browse files
committed
add go example
1 parent 0fae00b commit ed88d0e

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ An echo bot in multiple languages to get you started.
77
| Language | core version |
88
| --------------------------------------------- | ------------ |
99
| [C](./c) | `1.101.0` |
10+
| [Go](./go) | `1.110.0` |
1011
| [node.js over cffi (legacy)](./nodejs_cffi) | `1.101.0` |
1112
| [node.js over jsonrpc](./nodejs_napi_jsonrpc) | `1.101.0` |
1213
| [Python](./python) | `1.94.0` |

go/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
accounts

go/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Echo Bot - Go
2+
3+
### Installing deltachat-rpc-server
4+
5+
For the bot to work, first `deltachat-rpc-server` program needs to
6+
be installed and available in your `PATH`. To install it from source run:
7+
8+
```sh
9+
cargo install --git https://github.com/deltachat/deltachat-core-rust/ deltachat-rpc-server
10+
```
11+
12+
For more info and pre-built binaries check:
13+
https://github.com/deltachat/deltachat-core-rust/tree/master/deltachat-rpc-server
14+
15+
### Using the bot
16+
17+
To run the bot, the first time you need to pass the credentials
18+
as command line arguments:
19+
20+
```sh
21+
go run . $yourEmail $yourPassword
22+
```
23+
24+
This will create a subdirectory called `accounts` in the current
25+
working directory, this is where deltachat stores the state. The
26+
credentials will be stored there so further invocations do not need
27+
them specified again:
28+
29+
```sh
30+
go run .
31+
```
32+
33+
Open a chat with the bot address in your Delta Chat and write some messages
34+
to test the bot.

go/echobot.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
7+
"github.com/deltachat/deltachat-rpc-client-go/deltachat"
8+
)
9+
10+
func logEvent(event *deltachat.Event) {
11+
log.Printf("%v: %v", event.Type, event.Msg)
12+
}
13+
14+
func main() {
15+
rpc := deltachat.NewRpcIO()
16+
defer rpc.Stop()
17+
rpc.Start()
18+
19+
manager := &deltachat.AccountManager{rpc}
20+
sysinfo, _ := manager.SystemInfo()
21+
log.Println("Running deltachat core", sysinfo["deltachat_core_version"])
22+
23+
bot := deltachat.NewBotFromAccountManager(manager)
24+
bot.On(deltachat.EVENT_INFO, logEvent)
25+
bot.On(deltachat.EVENT_WARNING, logEvent)
26+
bot.On(deltachat.EVENT_ERROR, logEvent)
27+
bot.OnNewMsg(func(msg *deltachat.Message) {
28+
snapshot, _ := msg.Snapshot()
29+
chat := deltachat.Chat{bot.Account, snapshot.ChatId}
30+
chat.SendText(snapshot.Text)
31+
})
32+
33+
if !bot.IsConfigured() {
34+
log.Println("Bot not configured, configuring...")
35+
err := bot.Configure(os.Args[1], os.Args[2])
36+
if err != nil {
37+
log.Fatalln(err)
38+
}
39+
}
40+
41+
addr, _ := bot.GetConfig("addr")
42+
log.Println("Listening at:", addr)
43+
bot.RunForever()
44+
}

go/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/deltachat-bot/echo/go
2+
3+
go 1.19
4+
5+
require github.com/deltachat/deltachat-rpc-client-go v0.1.0
6+
7+
require (
8+
github.com/creachadair/jrpc2 v0.44.0 // indirect
9+
golang.org/x/sync v0.1.0 // indirect
10+
)

go/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/creachadair/jrpc2 v0.44.0 h1:O7H8XMliOemo2u598dN7Knld8K3TZgOnY9Mm0IIqt28=
2+
github.com/creachadair/jrpc2 v0.44.0/go.mod h1:mVyD3HybrKVaZqFNRVGZGWH9Gd1OXtnmfqZT7T1oLQw=
3+
github.com/deltachat/deltachat-rpc-client-go v0.1.0 h1:vdJbAQh7OGaXgRcFwQLrsclA1RQdPV7eF0CxxmpGqzI=
4+
github.com/deltachat/deltachat-rpc-client-go v0.1.0/go.mod h1:LiHW4HxGGwP4yfO5qZWRf26KwYVoUz5IrIkShHuNpWM=
5+
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
6+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
7+
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
8+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

0 commit comments

Comments
 (0)