Skip to content
/ tg-bot-template Public template

Template repo with boilerplate code to write Telegram bots in Go

License

Notifications You must be signed in to change notification settings

nskondratev/tg-bot-template

Repository files navigation

tg-bot-template

Template repo with boilerplate code to write Telegram bots in Go.

What is this repo for?

I implemented several bots for Telegram and each time I started by writing/copying boilerplate code. This repository is a template for a quick start of a new bot. It solves the following problems:

In short, this template will save you a couple of hours and allow you to immediately start implementing the bot's logic.

Quickstart

  1. Press "Use this template" button at the top or just follow the link.
  2. Clone the generated repository to your machine.
  3. Rename module and change import paths by calling the command (replace github.com/author/newbot with yours repo name):
./scripts/rename.sh github.com/author/newbot
  1. Fill configuration in .env file:
mv .env.example .env && nano .env
  1. Run your bot locally:
make run

To set up a webhook for receiving updates, fill the config in .env file and run the following command:

./scripts/set_webhook.sh

To clear a webhook run the same script with -c flag provided:

./scripts/set_webhook.sh -c

Next steps

Project structure

  • bin - dir for compiled binary deps (look at the tools directory).
  • cmd/bot - entry-point for running bot locally.
  • internal:
    • internal/app - contains business-logic layer and adapters to external world in sub-packages.
    • internal/bot - wrappers to work with Telegram Bot API and middlewares implementation.
    • internal/bot/handlers - handlers for different update types.
    • internal/bot/middleware - middlewares for all updates.
    • internal/boot - bootstrapping code for bot creation (used in local entry-point and Google Cloud Function).
    • internal/env - utilities for getting env-vars values.
    • internal/logger - logger creation code and custom log fields constants.
    • internal/metrics - metrics client creation code and registering of custom metrics.
  • scripts - handy scripts for renaming module, changing import paths and setting up webhook URL.
  • tools - binary deps of a project.

Releases

No releases published

Packages

No packages published