leopku / telenow

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

telego logo

telego

Docker Go template for creating Telegram Bots with clean architecture.

TravisCI Codacy Software License SayThanks.io

Table of Contents

Usage

Create Bot

Create new bot with @BotFather.

Copy Token

Create file ./docker-compose.secret.yaml with the following structure and paste the token from @BotFather:

# ./docker-compose.secret.yaml

version: '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:

# ...
environment:
  TELEGO_NOTIFIER_BOT_TOKEN: "<token>"
  TELEGO_NOTIFIER_CHAT_ID: "<chat_id>"

Code

Logic

Main logic of the bot should be implemented inside Configure function in bot/bot.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/. Here is an example from hello.go:

func (b *Handler) HandleHello(m *tb.Message) error {
    _, err := b.Bot.Send(m.Sender, "Hello, world!")
    return err
}

Run

docker-compose \
  -f docker-compose.yaml \
  -f docker-compose.secret.yaml \
  up --build -d

Debug

You can debug your code with Delve debugger.

Build & Run

To enable dlv debugger inside the container run:

docker-compose \
  -f docker-compose.yaml \ 
  -f docker-compose.dev.yaml \
  -f docker-compose.secret.yaml \
  up --build -d

Attach

${GOPATH}/bin/dlv connect localhost:40000

About

License:MIT License


Languages

Language:Go 99.3%Language:Dockerfile 0.7%