-
Notifications
You must be signed in to change notification settings - Fork 835
/
libretaxi.go
176 lines (137 loc) Β· 4.23 KB
/
libretaxi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"database/sql"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/leonelquinteros/gotext"
_ "github.com/lib/pq" // important
"go.uber.org/ratelimit"
"libretaxi/callback"
"libretaxi/config"
"libretaxi/context"
"libretaxi/menu"
"libretaxi/rabbit"
"libretaxi/repository"
"libretaxi/sender"
"log"
"math/rand"
"time"
)
func initContext() *context.Context {
log.Printf("Using '%s' telegram token\n", config.C().Telegram_Token)
log.Printf("Using '%s' database connection string", config.C().Db_Conn_Str)
log.Printf("Using '%s' RabbitMQ connection string", config.C().Rabbit_Url)
context := &context.Context{}
bot, err := tgbotapi.NewBotAPI(config.C().Telegram_Token)
if err != nil {
log.Panic(err)
}
// bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
db, err := sql.Open("postgres", config.C().Db_Conn_Str)
if err != nil {
log.Fatal(err)
} else {
log.Print("Successfully connected to the database")
}
context.Bot = bot
context.Repo = repository.NewRepository(db)
context.Config = config.C()
return context
}
// Message producer (app logic)
func main1() {
context := initContext()
context.RabbitPublish = rabbit.NewRabbitClient(config.C().Rabbit_Url, "messages")
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
u.Limit = 99
updates, _ := context.Bot.GetUpdatesChan(u)
for update := range updates {
if update.Message != nil {
// Ignore the case where message comes from a chat, not from user. We do not support chats.
if update.Message.From == nil {
continue
}
userId := update.Message.Chat.ID
log.Printf("[%d - %s] %s", userId, update.Message.From.UserName, update.Message.Text)
menu.HandleMessage(context, userId, update.Message)
} else if update.CallbackQuery != nil {
// A couple of places where we directly interact with Telegram API without a queue. Not a good thing,
// must be enqueued as well.
cb := update.CallbackQuery
context.Bot.AnswerCallbackQuery(tgbotapi.NewCallback(cb.ID, "π OK"))
emptyKeyboard := tgbotapi.NewInlineKeyboardMarkup([]tgbotapi.InlineKeyboardButton{})
removeButton := tgbotapi.NewEditMessageReplyMarkup(cb.Message.Chat.ID, cb.Message.MessageID, emptyKeyboard)
_, err := context.Bot.Send(removeButton)
if err != nil {
log.Println(err)
}
callback.NewTgCallbackHandler().Handle(context, cb.Data)
}
}
}
// Message consumer (send to Telegram respecting rate limits)
func main2() {
context := initContext()
context.RabbitConsume = rabbit.NewRabbitClient(config.C().Rabbit_Url, "messages")
s := sender.NewSender(context)
s.Start()
}
func getLocale(languageCode string) *gotext.Locale {
locale := gotext.NewLocale("./locales", "all")
if languageCode == "ru" || languageCode == "es" {
locale.AddDomain(languageCode)
} else {
locale.AddDomain("en")
}
return locale
}
func massAnnounce() {
context := &context.Context{}
db, err := sql.Open("postgres", config.C().Db_Conn_Str)
if err != nil {
log.Fatal(err)
} else {
log.Print("Successfully connected to the database")
}
context.Repo = repository.NewRepository(db)
context.Config = config.C()
context.RabbitPublish = rabbit.NewRabbitClient(config.C().Rabbit_Url, "messages")
var userId int64
var languageCode string
rows, err := db.Query("select \"userId\", \"languageCode\" from users where \"languageCode\"='pt-br'")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
rl := ratelimit.New(5) // don't load DB too much
for rows.Next() {
err := rows.Scan(&userId, &languageCode)
if err == nil && context.Repo.ShowCallout(userId, "pt_br_translation_announcement") {
locale := getLocale(languageCode)
link := locale.Get("main.welcome_link")
text := link + " πππ /start πππ"
msg := tgbotapi.NewMessage(userId, text)
context.RabbitPublish.PublishTgMessage(rabbit.MessageBag{
Message: msg,
Priority: 0, // LOWEST
})
log.Println("Mass sending to ", userId, languageCode)
context.Repo.DismissCallout(userId, "pt_br_translation_announcement")
rl.Take()
}
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
}
func main() {
rand.Seed(time.Now().UnixNano())
config.Init("libretaxi")
go main1()
go main2()
//go massAnnounce()
forever := make(chan bool)
<- forever
}