At this project, I'm trying to create a fully extendable golang template for creating telegram bot with Clean structure and come up with a reusable, nice and scalable base for any telegram project and ofcourse reusable codes and some separate packages.
If you do love to contribute, please do, I appreciate it.
Don't forget to read CONTRIBUTING file to know about how to contribute in this project.
- Install Dependencies:
-
go get -v github.com/rubenv/sql-migrate/... go mod tidy
- Note: For migrations you most likely need
sql-migrate
.
-
- Copy and paste these lines in your terminal when you're inside project root directory:
-
cp dbconfig_example.yml dbconfig.yml cp env_example.yml env.yml
- Those example files(env_example.yml and dbconfig_example.yml) have ready configurations for a quick start for the project.
-
- Change value of
bot_token
anddeveloper_id
by your own bot token and your own id which you can get from userinfobot. - How to run:
-
go run ./cmd/main/main.go
-
If you don't have any idea about clean structure, please just take a moment and just have a look at it first and after understanding the structure, come back here and continue.
Note: If you know about clean structure, understanding folders usages will make more sense.
The cfg.go
file (if the config files are passed just right) fills attributes in global.go
.
Just to load everything right up, you have to import cfg.go
file like:
_ "github.com/maktoobgar/go_telegram_template/internal/app/load"
Note: You don't need to do that, I'm just describing what is happening.
There is a file named config.go inside internal/config
folder which defines structures for config files of the project.
Main config files, which have some default configs in them and no coding happens in them, are inside build
folder and the default config file named config.yaml
is inside build/config
address.
Take a look at them and you get whats going on.
g
stands for global. In this project, I created a file named global.go
inside internal/global
folder to have all config files for the project in one place and access them as fast as possible.
In down documentation you may see me mention using g.Something
, just know that I meant global.go file and I assume you know that you have to import it like g "github.com/maktoobgar/go_telegram_template/internal/global"
to use g
.
If you want to update default config file(config.yaml
file), you may add a file to the root project directory named env.yaml
or env.yml
and override any configs you need to and replace you new desired configs.
If you need to translate anything in your application, use g.Translator
object to do it and don't forget to add your translation to build/translations
folder like other files that you can use as an example in that directory.
Note: If you want to be so contractual, you can have interface of g.Translator
by calling g.Trans()
function and then do your translations like:
g.Trans().TranslateEN("my word")
If you need new languages(Persian and English added by default), you can add them by first updating languages
variable inside cfg.go file and then at least create an empty file for that variable inside build/translations
folder like translation.fa.json file.
In this project I added goqu
query builder, if you need to learn how to use it, read goqu example files inside their repo. (files followed by _example_test.go
at the end are example files)
If you take a look at config.go file, you can see that in Database
structure, there is a Name
attribute inside them.
That Name
attribute will be used to give you an access to your databases, after config files successfully loaded, you can use Name
attribute of your database to access their query builder to make changes on your databases just by calling g.Postgres[Name]
or g.Sqlite[Name]
or g.MySQL[Name]
or g.SqlServer[Name]
.
Please attention that a database connection with their Name
equal to main
, will be places inside g.DB
separately from others just for fast access to your main database connection.
Note: Connections to these databases are inside g.PostgresCons
, g.SqliteCons
, g.MySQLCons
and g.SqlServerCons
.
The services path is internal/services
and inside that you can create a folder, name it whatever you want and add a file in it and start writing you service. There are examples in internal/services
folder that you can have a look at them.
Note: I strongly recommend you to add a service for every tables you have in your database.
Note: If you want to follow up some more recommendations, create your interfaces or your popular structures(you use them somewhere else) for your services inside internal/contract
folder.