Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mitinarseny/telego
Template for creating Telegram Bots with clean architecture.
https://github.com/mitinarseny/telego
docker go telegram
Last synced: 2 months ago
JSON representation
Template for creating Telegram Bots with clean architecture.
- Host: GitHub
- URL: https://github.com/mitinarseny/telego
- Owner: mitinarseny
- License: mit
- Created: 2019-07-24T13:50:53.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-09-28T22:48:36.000Z (over 5 years ago)
- Last Synced: 2024-06-20T03:49:36.232Z (7 months ago)
- Topics: docker, go, telegram
- Language: Go
- Homepage:
- Size: 609 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
telego
Docker Go template for creating Telegram Bots with clean architecture.
## Table of Contents
* [Usage](#usage)
* [Create Bot](#create-bot)
* [Copy Token](#copy-token)
* [Notifier](#notifier)
* [Code](#code)
* [Logic](#logic)
* [Handlers](#handlers)
* [Run](#run)
* [Debug](#debug)
* [Build & Run](#build--run)
* [Attach](#attach)
## Usage
### Create Bot
Create new bot with [@BotFather](https://t.me/BotFather).
### Copy Token
Create file `./docker-compose.secret.yaml` with the following structure and paste the token from [@BotFather](https://t.me/BotFather):
```yaml
# ./docker-compose.secret.yamlversion: '3.7'
services:
bot:
environment:
TELEGO_BOT_TOKEN: "12345689:ABCdEFgHi1JKLMNO23P45rSTU6vw78xyz-a"
```
#### Notifier
You can enable Telegram notifications on your bot's status (`UP` or `DOWN`) by creating another bot and a group chat with this bot. Then edit `./docker-compose.secret.yaml`:
```yaml
# ...
environment:
TELEGO_NOTIFIER_BOT_TOKEN: ""
TELEGO_NOTIFIER_CHAT_ID: ""
```
### Code
#### Logic
Main logic of the bot should be implemented inside `Configure` function in [`bot/bot.go`](bot/bot.go):
```go
func Configure(b *tb.Bot) (*tb.Bot, error) {
h := handlers.Handler{Bot: b}
b.Handle("/hello", withLogMsg(h.HandleHello))
return b, nil
}func withLogMsg(handler func(*tb.Message) error) func(message *tb.Message) {
return func(m *tb.Message) {
if err := handler(m); err != nil {
log.WithFields(log.Fields{
"context": "BOT",
"handler": handler,
}).Error(err)
}
}
}
```
#### Handlers
All hanlders should be placed in [`bot/handlers/`](bot/handlers). Here is an example from [`hello.go`](bot/handlers/hello.go):
```go
func (b *Handler) HandleHello(m *tb.Message) error {
_, err := b.Bot.Send(m.Sender, "Hello, world!")
return err
}
```
### Run
```bash
docker-compose \
-f docker-compose.yaml \
-f docker-compose.secret.yaml \
up --build -d
```## Debug
You can debug your code with [Delve](https://github.com/go-delve/delve) debugger.
### Build & Run
To enable [dlv](https://github.com/go-delve/delve) debugger inside the container run:
```bash
docker-compose \
-f docker-compose.yaml \
-f docker-compose.dev.yaml \
-f docker-compose.secret.yaml \
up --build -d
```
### Attach
```bash
${GOPATH}/bin/dlv connect localhost:40000
```