Skip to content

Commit

Permalink
one executable file
Browse files Browse the repository at this point in the history
  • Loading branch information
iliayatsenko committed Sep 8, 2024
1 parent d8ade12 commit c462fa8
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 179 deletions.
7 changes: 0 additions & 7 deletions .env

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
vendor
vendor
dist/
29 changes: 29 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com

# The lines below are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/need to use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

version: 2

before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...

builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

archives:
- files:
- none*

26 changes: 0 additions & 26 deletions cmd/en_to_ru/main.go

This file was deleted.

26 changes: 0 additions & 26 deletions cmd/en_to_ua/main.go

This file was deleted.

26 changes: 0 additions & 26 deletions cmd/ru_to_en/main.go

This file was deleted.

26 changes: 0 additions & 26 deletions cmd/ru_to_ua/main.go

This file was deleted.

26 changes: 0 additions & 26 deletions cmd/ua_to_en/main.go

This file was deleted.

26 changes: 0 additions & 26 deletions cmd/ua_to_ru/main.go

This file was deleted.

46 changes: 31 additions & 15 deletions internal/tgbot/tgbot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,32 @@ var staticKeyboard = tgbotapi.NewReplyKeyboard(
)

type TgBot struct {
token string
greeting string
errorMsg string
translator *translator.Translator
Name string
Link string
token string
greeting string
errorMsg string
translator *translator.Translator
otherBotsDiscoverFunc func() map[string]string
}

func New(token, greeting, errorMsg string, translator *translator.Translator) *TgBot {
func New(
name,
link,
token,
greeting,
errorMsg string,
translator *translator.Translator,
otherBotsDiscoverFunc func() map[string]string,
) *TgBot {
return &TgBot{
token: token,
greeting: greeting,
errorMsg: errorMsg,
translator: translator,
Name: name,
Link: link,
token: token,
greeting: greeting,
errorMsg: errorMsg,
translator: translator,
otherBotsDiscoverFunc: otherBotsDiscoverFunc,
}
}

Expand Down Expand Up @@ -66,14 +80,16 @@ func (t *TgBot) PollTgApiAndRespond() {
if update.Message.IsCommand() {
switch update.Message.Command() {
case "otherBots":
otherBots := t.otherBotsDiscoverFunc()

otherBotButtons := []tgbotapi.InlineKeyboardButton{}
for name, link := range otherBots {
otherBotButtons = append(otherBotButtons, tgbotapi.NewInlineKeyboardButtonURL(name, link))
}

var otherBotsKeyboard = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonURL("Ru-Ua", "tg://resolve?domain=@RuToUaTranslatorBot"),
tgbotapi.NewInlineKeyboardButtonURL("Ua-Ru", "tg://resolve?domain=@UaToRuTranslatorBot"),
tgbotapi.NewInlineKeyboardButtonURL("En-Ru", "tg://resolve?domain=@EnToRuTranslatorBot"),
tgbotapi.NewInlineKeyboardButtonURL("Ru-En", "tg://resolve?domain=@RuToEnTranslatorBot"),
tgbotapi.NewInlineKeyboardButtonURL("En-Ua", "tg://resolve?domain=@EnToUaTranslatorBot"),
tgbotapi.NewInlineKeyboardButtonURL("Ua-En", "tg://resolve?domain=@UaToEnTranslatorBot"),
otherBotButtons...,
),
)
replyMsg.ReplyMarkup = otherBotsKeyboard
Expand Down
118 changes: 118 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package main

import (
"github.com/joho/godotenv"
"os"
"sync"
"tg_translate_bots/internal/tgbot"
"tg_translate_bots/internal/translator"
"tg_translate_bots/internal/translator/client"
)

func main() {
_ = godotenv.Load(".env")

var botsPtr *[]*tgbot.TgBot
botsDiscoverFunc := func() map[string]string {
botsMap := make(map[string]string)
for _, bot := range *botsPtr {
botsMap[bot.Name] = bot.Link
}
return botsMap
}

var bots = []*tgbot.TgBot{
tgbot.New(
"En-Ru",
"tg://resolve?domain=@EnToRuTranslatorBot",
os.Getenv("EN_TO_RU_TG_BOT_TOKEN"),
"Hello. I translate all messages from English to Russian.",
"An error occurred while translating the message.",
translator.New(
"EN",
"RU",
&client.DeeplClient{},
),
botsDiscoverFunc,
),
tgbot.New(
"En-Ua",
"tg://resolve?domain=@EnToUaTranslatorBot",
os.Getenv("EN_TO_UA_TG_BOT_TOKEN"),
"Hello. I translate all messages from English to Ukrainian.",
"An error occurred while translating the message.",
translator.New(
"EN",
"UK",
&client.DeeplClient{},
),
botsDiscoverFunc,
),
tgbot.New(
"Ru-En",
"tg://resolve?domain=@RuToEnTranslatorBot",
os.Getenv("RU_TO_EN_TG_BOT_TOKEN"),
"Привет. Я перевожу все сообщения с русского на английский язык.",
"Произошла ошибка при переводе сообщения.",
translator.New(
"RU",
"EN",
&client.DeeplClient{},
),
botsDiscoverFunc,
),
tgbot.New(
"Ru-Ua",
"tg://resolve?domain=@RuToUaTranslatorBot",
os.Getenv("RU_TO_UA_TG_BOT_TOKEN"),
"Привет. Я перевожу все сообщения с русского на украинский язык.",
"Произошла ошибка при переводе сообщения.",
translator.New(
"RU",
"UK",
&client.DeeplClient{},
),
botsDiscoverFunc,
),
tgbot.New(
"Ua-En",
"tg://resolve?domain=@UaToEnTranslatorBot",
os.Getenv("UA_TO_EN_TG_BOT_TOKEN"),
"Привіт. Я перекладаю всі повідомлення з української на англійську мову.",
"Сталася помилка під час перекладу повідомлення.",
translator.New(
"UK",
"EN",
&client.DeeplClient{},
),
botsDiscoverFunc,
),
tgbot.New(
"Ua-Ru",
"tg://resolve?domain=@UaToRuTranslatorBot",
os.Getenv("UA_TO_RU_TG_BOT_TOKEN"),
"Привіт. Я перекладаю всі повідомлення з української на російську мову.",
"Сталася помилка під час перекладу повідомлення.",
translator.New(
"UK",
"RU",
&client.DeeplClient{},
),
botsDiscoverFunc,
),
}

botsPtr = &bots

var wg sync.WaitGroup
wg.Add(len(bots))

for _, bot := range bots {
go func(bot *tgbot.TgBot) {
defer wg.Done()
bot.PollTgApiAndRespond()
}(bot)
}

wg.Wait()
}

0 comments on commit c462fa8

Please sign in to comment.