Skip to content

Commit

Permalink
add webhook support
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalvatori committed Jan 1, 2021
1 parent 7d28feb commit 3aa89d5
Show file tree
Hide file tree
Showing 17 changed files with 462 additions and 115 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ zbot-telegram
*.db
/venv
zbot_dev.conf
plex.py
plex.py
vendor/**
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ script:
- go test -v github.com/ssalvatori/zbot-telegram/utils -coverprofile=utils.coverprofile
- go test -v github.com/ssalvatori/zbot-telegram/user -coverprofile=user.coverprofile
- go test -v github.com/ssalvatori/zbot-telegram/db -coverprofile=db.coverprofile
- go test -v github.com/ssalvatori/zbot-telegram/commands -coverprofile=db.coverprofile
- go test -v github.com/ssalvatori/zbot-telegram/commands -coverprofile=commands.coverprofile
- go test -v github.com/ssalvatori/zbot-telegram/server -coverprofile=server.coverprofile
- $HOME/gopath/bin/gover
- $HOME/gopath/bin/goveralls -coverprofile gover.coverprofile -service travis-ci --ignore=db/mock.go,db/db.go,db/sqlite.go
after_success:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ARG ARCH=amd64

WORKDIR /go/src/app
COPY . .
RUN CGO_ENABLED=1 GOOS=${OS} GOARCH=${ARCH} go build -ldflags "-X github.com/ssalvatori/zbot-telegram/zbot.version=`git describe --tags` -X github.com/ssalvatori/zbot-telegram/zbot.buildTime=`TZ=UTC date -u '+%Y-%m-%d %H:%M:%SZ'` -X github.com/ssalvatori/zbot-telegram/zbot.gitHash=`git rev-parse --short HEAD`" -o zbot-telegram-${OS}-${ARCH}
RUN CGO_ENABLED=1 GOOS=${OS} GOARCH=${ARCH} go build -ldflags "-X github.com/ssalvatori/zbot-telegram/zbot.version=`git describe --tags` -X github.com/ssalvatori/zbot-telegram/zbot.buildTime=`TZ=UTC date -u '+%Y-%m-%dT%H:%M:%SZ'` -X github.com/ssalvatori/zbot-telegram/zbot.gitHash=`git rev-parse --short HEAD`" -o zbot-telegram-${OS}-${ARCH}

FROM debian:buster-slim

Expand Down
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@
[![Coverage Status](https://coveralls.io/repos/github/ssalvatori/zbot-telegram/badge.svg?branch=master)](https://coveralls.io/github/ssalvatori/zbot-telegram?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/ssalvatori/zbot-telegram)](https://goreportcard.com/report/github.com/ssalvatori/zbot-telegram)



## Features

* Core Learn module [doc/learn](doc/learn.md)
* Run external modules
* Webhook [doc/webhook](doc/webhook.md)

## Requirements

* You need to get an API TOKEN from [BotFather@Telegram](https://core.telegram.org/bots)

## Setup

You **must** set the environment variable **ZBOT_CONFIG_FILE** with the path to the configuration file
You **must** set the **ZBOT_CONFIG_FILE** environment variable with the path to the configuration file
* ZBOT_CONFIG_FILE : Path to the configuration file (default ./zbot.conf)
* ZBOT_LOG_LEVEL : Log verbosity (default info)

## Configuration File

Expand All @@ -24,7 +31,21 @@ zbot:
level: false
db:
engine: sqlite
name: db_name
file: path_to_sqlite_file.db
host: 127.0.0.1
port: 3306
username: db_username
password: db_password
webhook:
disable: true
port: 13371
auth:
- channel: channel1
id: 1234
token: <YOUR_SECURE_TOKEN>
- channel: channel2
token: <YOUR_SECURE_TOKEN>s
commands:
learn:
disabled:
Expand Down Expand Up @@ -52,11 +73,11 @@ modules:

## Database Schemas

[GORM](https://gorm.io/index.html), will creat the necessary schemas and migrations
[GORM](https://gorm.io/index.html), will creat all the schemas and migrations

## Development

```bash
docker build -t zbot-telegram -f Dockerfile .
docker run --rm --name zbot-telegram -v ${PWD}/modules:/app/modules -v ${PWD}/zbot.conf:/app/zbot.conf -e ZBOT_CONFIG_FILE=/app/zbot.conf zbot-telegram:latest
```
```
36 changes: 31 additions & 5 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"io/ioutil"

log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"gopkg.in/yaml.v3"
)

/*
Expand All @@ -17,9 +17,17 @@ type Configuration struct {
Level bool `yaml:"level"`
} `yaml:"zbot"`
Db struct {
Engine string `yaml:"engine"`
File string `yaml:"file"`
Engine string `yaml:"engine"`
File string `yaml:"file"`
} `yaml:"db"`
Webhook struct {
Port int `yaml:"port"`
Auth []struct {
Channel string `yaml:"channel"`
ID int64 `yaml:"id,omitempty"`
Token string `yaml:"token,omitempty"`
} `yaml:"auth"`
} `yaml:"webhook"`
Commands struct {
Learn struct {
Disabled []string `yaml:"disabled"`
Expand All @@ -42,10 +50,23 @@ type Configuration struct {
type Configuration struct {
Zbot configurationZbot `yaml:"zbot"`
Db configurationDb `yaml:"db"`
Webhook configurationWebhook `yaml:"webhook"`
Commands configurationCommands `yaml:"commands"`
Modules configurationModules `yaml:"modules"`
}

type configurationWebhook struct {
Disable bool `yaml:"disable,omitempty"`
Port int `yaml:"port"`
Auth []channel `yaml:"auth"`
}

type channel struct {
Channel string `yaml:"channel"`
ID int64 `yaml:"id,omitempty"`
Token string `yaml:"token,omitempty"`
}

type configurationCommands struct {
Learn configurationLearn `yaml:"learn"`
Disabled []string `yaml:"disabled"`
Expand All @@ -62,8 +83,13 @@ type configurationZbot struct {
}

type configurationDb struct {
Engine string `yaml:"engine"`
File string `yaml:"file"`
Engine string `yaml:"engine"`
Name string `yaml:"name"`
File string `yaml:"file"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}

type configurationLearn struct {
Expand Down
66 changes: 66 additions & 0 deletions doc/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Zbot Configuration

```yaml
zbot:
token: <TELEGRAM_TOKEN>
ignore_duration: 300
ignore: true
level: false
db:
engine: sqlite
file: path_to_sqlite_file.db
webhook:
disable: true
port: 13371
auth:
- channel: channel1
id: 1234
token: <YOUR_SECURE_TOKEN>
- channel: channel2
token: <YOUR_SECURE_TOKEN>
commands:
learn:
disabled:
- zbot_dev
disabled:
- ignore
- level
- forget
modules:
path: ./modules/
list:
- key: crypto
file: cypto
description: get some crypto data
- key: test
file: test
description: test module
- key: temp
file: temp.sh
description: get weather info
- key: plex
file: plex2.py
description: get plext information
```

## zbot

## db
```yaml
db:
engine: sqlite
file: path_to_sqlite_file.db
```
## webhook
```yaml
webhook:
disable: bool // Enable or disable bot webhook (default: false)
port: int // Webhook port (default: 11337)
auth:
- channel: string // Channel name (bot will overwrite it using chat_id information)
id: int64 // Telegram Chat_ID (leave it empty and the bot will try to get it using channel name)
token: string // Token to autenticate request, this should be unique per channel
```
## commands

## modules
57 changes: 57 additions & 0 deletions doc/learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Learn Module

## Configuiration
This set of modules are enabled by default but they can be disabled in the bot [configuration file](./configuration.md)

### Learn a *term*
If the term is already in the db an autoincrementa number will be added as a suffix. (term1, term2,...)
```
!learn [term] [meaning]
```

### Get the *meaning* of a *term*
```
?[term]
```

### Get information a *term*
```
!who [term]
```

### Append *meaning* to a *term*
```
!append [term] [meaning]
```

### Get a random *term*
```
!rand
```

### Get last term learned
```
!last
```

### Search similar terms
```
!search *term*
```

### Find terms searching a *text* inside a meaning definition
```
!find *text*
```

### Top (get top [number] terms)
```
!top [number]
```


### Get total number of terms in db
```
!stats
```

Empty file added doc/webhook.md
Empty file.
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ go 1.15

require (
github.com/caarlos0/env/v6 v6.4.0
github.com/mitchellh/mapstructure v1.4.0
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.6.1
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 // indirect
golang.org/x/sys v0.0.0-20201231184435-2d18734c6014 // indirect
gopkg.in/tucnak/telebot.v2 v2.3.5
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v2 v2.2.2
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.20.8
gorm.io/gorm v1.20.9
)
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ=
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/mitchellh/mapstructure v1.4.0 h1:7ks8ZkOP5/ujthUsT07rNv+nkLXCQWKNHuwzOAesEks=
github.com/mitchellh/mapstructure v1.4.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand All @@ -24,20 +26,20 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742 h1:+CBz4km/0KPU3RGTwARGh/noP3bEwtHcq+0YcBQM2JQ=
golang.org/x/sys v0.0.0-20201218084310-7d0127a74742/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201231184435-2d18734c6014 h1:joucsQqXmyBVxViHCPFjG3hx8JzIFSaym3l3MM/Jsdg=
golang.org/x/sys v0.0.0-20201231184435-2d18734c6014/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/tucnak/telebot.v2 v2.3.5 h1:TdMJTlG8kvepsvZdy/gPeYEBdwKdwFFjH1AQTua9BOU=
gopkg.in/tucnak/telebot.v2 v2.3.5/go.mod h1:BgaIIx50PSRS9pG59JH+geT82cfvoJU/IaI5TJdN3v8=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.8 h1:iToaOdZgjNvlc44NFkxfLa3U9q63qwaxt0FdNCiwOMs=
gorm.io/gorm v1.20.8/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gorm.io/gorm v1.20.9 h1:M3aIZKXAC1PtPVu9t3WGwkBTE1le5c2telz3I/qjRNg=
gorm.io/gorm v1.20.9/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
34 changes: 34 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,40 @@ func setup() {

zbot.Db = setupDatabase(configuration)
zbot.ExternalModules = zbot.ExternalModulesList(configuration.Modules.List)
zbot.Channels = setupChannels(configuration.Webhook.Auth)

if configuration.Webhook.Disable {
log.Info(fmt.Sprintf("WebServer: disable"))
zbot.Webhook.Enable = false
} else {
zbot.Webhook.Enable = true
log.Info(fmt.Sprintf("WebServer: enable"))

if len(configuration.Webhook.Auth) == 0 {
log.Fatal("No Webhook.Auth present, exiting now!!")
}

if configuration.Webhook.Port != 0 {
zbot.Webhook.Port = configuration.Webhook.Port
}
log.Info(fmt.Sprintf("WebServer Port: %d", zbot.Webhook.Port))

}

}

func setupChannels(configuration []channel) []zbot.Channel {
var channels = []zbot.Channel{}

for i := range configuration {
channels = append(channels, zbot.Channel{
ID: configuration[i].ID,
Title: configuration[i].Channel,
AuthToken: configuration[i].Token,
})
}

return channels
}

func init() {
Expand Down

0 comments on commit 3aa89d5

Please sign in to comment.