https://github.com/nskondratev/tg-bot-template
Template repo with boilerplate code to write Telegram bots in Go
https://github.com/nskondratev/tg-bot-template
go golang google-cloud google-cloud-functions telegram-bot template-project
Last synced: 7 months ago
JSON representation
Template repo with boilerplate code to write Telegram bots in Go
- Host: GitHub
- URL: https://github.com/nskondratev/tg-bot-template
- Owner: nskondratev
- License: mit
- Created: 2020-12-30T13:27:02.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-01-10T08:39:55.000Z (about 5 years ago)
- Last Synced: 2023-03-03T15:23:28.116Z (about 3 years ago)
- Topics: go, golang, google-cloud, google-cloud-functions, telegram-bot, template-project
- Language: Go
- Homepage:
- Size: 3.03 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tg-bot-template
Template repo with boilerplate code to write [Telegram bots in](https://core.telegram.org/bots/api) 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:
- App structure: follows the [Standard Go Project Layout](https://github.com/golang-standards/project-layout).
- Handy middlewares for HTTP-like processing updates from Telegram.
- Deploying bot as a [Google Cloud Function](https://cloud.google.com/functions) and convenient local debugging with
polling updates.
- Structured logging with [zerolog](https://github.com/rs/zerolog) lib.
- Sending custom metrics with [OpenCensus](https://opencensus.io/) to Google Cloud. See more
in [docs](https://cloud.google.com/monitoring/custom-metrics/open-census?hl=en).
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](https://github.com/nskondratev/tg-bot-template/generate).
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):
```bash
./scripts/rename.sh github.com/author/newbot
```
4. Fill configuration in .env file:
```bash
mv .env.example .env && nano .env
```
5. Run your bot locally:
```bash
make run
```
To set up a webhook for receiving updates, fill the config in `.env` file and run the following command:
```bash
./scripts/set_webhook.sh
```
To clear a webhook run the same script with `-c` flag provided:
```bash
./scripts/set_webhook.sh -c
```
## Next steps
* Add domain-specific logic in [internal/app](./internal/app) package.
* Add update handlers in [internal/app/bot/handlers](./internal/app/bot/handlers) package.
* The library [telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api) is used to work with Telegram
Bot API.
## 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.