https://github.com/chatmail/rpc-client-go
Chatmail bindings for Golang
https://github.com/chatmail/rpc-client-go
bindings chatmail go golang
Last synced: about 1 year ago
JSON representation
Chatmail bindings for Golang
- Host: GitHub
- URL: https://github.com/chatmail/rpc-client-go
- Owner: chatmail
- License: mpl-2.0
- Created: 2023-01-30T18:10:47.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-03-21T20:13:20.000Z (about 1 year ago)
- Last Synced: 2025-04-10T00:32:16.516Z (about 1 year ago)
- Topics: bindings, chatmail, go, golang
- Language: Go
- Homepage:
- Size: 285 KB
- Stars: 9
- Watchers: 7
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Chatmail API for Go

[](https://pkg.go.dev/github.com/deltachat/deltachat-rpc-client-go)
[](https://github.com/deltachat/deltachat-rpc-client-go/actions/workflows/ci.yml)

[](https://goreportcard.com/report/github.com/deltachat/deltachat-rpc-client-go)
Chatmail client & bot API for Golang.
## Install
```sh
go get -u github.com/deltachat/deltachat-rpc-client-go
```
### Installing deltachat-rpc-server
This package depends on a standalone Chatmail RPC server `deltachat-rpc-server` program that must be
available in your `PATH`. For installation instructions check:
https://github.com/deltachat/deltachat-core-rust/tree/master/deltachat-rpc-server
## Usage
Example echo-bot that will echo back any text message you send to it:
```go
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 runEchoBot(bot *deltachat.Bot, accId deltachat.AccountId) {
sysinfo, _ := bot.Rpc.GetSystemInfo()
log.Println("Running deltachat core", sysinfo["deltachat_core_version"])
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()
}
func main() {
trans := transport.NewIOTransport()
trans.Open()
defer trans.Close()
rpc := &deltachat.Rpc{Context: context.Background(), Transport: trans}
runEchoBot(deltachat.NewBot(rpc), deltachat.GetAccount(rpc))
}
```
Save the previous code snippet as `echobot.go` then run:
```sh
go mod init echobot; go mod tidy
go run ./echobot.go bot@example.com PASSWORD
```
Check the [examples folder](./examples)
for more examples.
## Developing bots faster ⚡
If what you want is to develop bots, you probably should use this library together with
[deltabot-cli-go](https://github.com/deltachat-bot/deltabot-cli-go/), it takes away the
repetitive process of creating the bot CLI and let you focus on writing your message processing logic.
## Testing your code
`deltachat.AcFactory` is provided to help users of this library to unit-test their code.
### Local mail server
You need to have a local fake email server running. The easiest way to do that is with Docker:
```
$ docker pull ghcr.io/deltachat/mail-server-tester:release
$ docker run -it --rm -p 3025:25 -p 3110:110 -p 3143:143 -p 3465:465 -p 3993:993 ghcr.io/deltachat/mail-server-tester
```
### Using AcFactory
After setting up the fake email server, create a file called `main_test.go` inside your tests folder,
and save it with the following content:
```go
package main // replace with your package name
import (
"testing"
"github.com/deltachat/deltachat-rpc-client-go/deltachat"
)
var acfactory *deltachat.AcFactory
func TestMain(m *testing.M) {
acfactory = &deltachat.AcFactory{}
acfactory.TearUp()
defer acfactory.TearDown()
m.Run()
}
```
Now in your other test files you can do:
```go
package main // replace with your package name
import (
"testing"
"github.com/deltachat/deltachat-rpc-client-go/deltachat"
"github.com/stretchr/testify/assert"
)
func TestEchoBot(t *testing.T) {
acfactory.WithOnlineBot(func(bot *deltachat.Bot, botAcc deltachat.AccountId) {
go runEchoBot(bot, botAcc) // this is the function we are testing
acfactory.WithOnlineAccount(func(uRpc *deltachat.Rpc, uAccId deltachat.AccountId) {
chatId := acfactory.CreateChat(uRpc, uAccId, bot.Rpc, botAcc)
uRpc.MiscSendTextMessage(uAccId, chatId, "hi")
msg := acfactory.NextMsg(uRpc, uAccId)
assert.Equal(t, "hi", msg.Text) // check that bot echoes back the "hi" message from user
})
})
}
```
### GitHub action
To run the tests in a GitHub action with the fake mail server service:
```yml
name: Test
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Run tests
run: |
go test -v
services:
mail_server:
image: ghcr.io/deltachat/mail-server-tester:release
ports:
- 3025:25
- 3143:143
- 3465:465
- 3993:993
```
Check the complete example at [examples/echobot_full](./examples/echobot_full)
## Contributing
Pull requests are welcome! check [CONTRIBUTING.md](./CONTRIBUTING.md)