This project provides a template for building Telegram bots, leveraging the gotgbot library, dependency injection with fx, and other helpful tools.
Clone the repository or use as template on github
git clone https://github.com/fullpipe/gotgbot-template.gitAdd your env vars
cp .env.dist .envMake sure you have task and gowatch installed
Start bot with
task devThis project uses the gotgbot library for interacting with the Telegram Bot API. Refer to the gotgbot documentation for details on using the library: gotgbot.
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)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),Simplifies common development tasks like running the application, generating mocks, and generating GraphQL code. See the Taskfile.yaml for available commands.
Mocks for testing are generated using mockery.
task mockeryFor 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 . graphOn 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',
},
},
};
}),
],
};Prometheus metrics are exposed at :9090/metrics, allowing monitoring of the application's performance.