Skip to content

fullpipe/gotgbot-template

Repository files navigation

gotgbot-template

This project provides a template for building Telegram bots, leveraging the gotgbot library, dependency injection with fx, and other helpful tools.

Getting Started

Clone the repository or use as template on github

git clone https://github.com/fullpipe/gotgbot-template.git

Add your env vars

cp .env.dist .env

Make sure you have task and gowatch installed

Start bot with

task dev

Key Features and Concepts

Gotgbot

This project uses the gotgbot library for interacting with the Telegram Bot API. Refer to the gotgbot documentation for details on using the library: gotgbot.

Dependency Injection (DI) with fx

The project utilizes the fx framework for dependency injection, promoting modularity and testability.

New repositories can be registered in di/repository.go

fx.Provide(repository.NewUserRepo)

Controllers

As you can see in controller/start.go. Controller is a struct which implements di.Controller interface.

Controllers are registered in cmd/bot/bot.go with di.AsController wrapper

di.AsController(controller.NewMessageController),

task

Simplifies common development tasks like running the application, generating mocks, and generating GraphQL code. See the Taskfile.yaml for available commands.

mockery

Mocks for testing are generated using mockery.

task mockery

GraphQL

For Telegram MiniApps api we use gqlgen.

Update schema in (api)[api], then run task gen to update/generate handlers.

To run GraphQL server

go run . graph

Auth

On your telegram miniapp client you should add Authorization header to all requests.

headers: {
    Authorization: `tma ${window.Telegram.WebApp.initData}`,
},
Angular + apollo/client example
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
import { provideHttpClient } from '@angular/common/http';
import { provideApollo } from 'apollo-angular';
import { ApolloLink, InMemoryCache } from '@apollo/client/core';
import { environment } from '../environments/environment';
import { setContext } from '@apollo/client/link/context';
import { ErrorResponse, onError } from '@apollo/client/link/error';
import { createUploadLink } from 'apollo-upload-client';
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';

export const appConfig: ApplicationConfig = {
  providers: [
    provideAnimationsAsync(),
    provideRouter(routes),
    provideHttpClient(),
    provideApollo(() => {
      const error = onError((e: ErrorResponse) => {
        console.log(e);
      });

      const auth = setContext((operation, context) => {
        if (!window.Telegram.WebApp.initData) {
          return {};
        } else {
          return {
            headers: {
              Authorization: `tma ${window.Telegram.WebApp.initData}`,
            },
          };
        }
      });

      return {
        link: ApolloLink.from([error, auth, createUploadLink({ uri: environment.graphUrl })]),
        cache: new InMemoryCache(),
        defaultOptions: {
          watchQuery: {
            fetchPolicy: 'no-cache',
            errorPolicy: 'ignore',
          },
          query: {
            fetchPolicy: 'no-cache',
            errorPolicy: 'all',
          },
        },
      };
    }),
  ],
};

Metrics

Prometheus metrics are exposed at :9090/metrics, allowing monitoring of the application's performance.

About

Telegram bot boilerplate

Topics

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors