-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·140 lines (117 loc) · 3.7 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/go-co-op/gocron"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/tobigiwa/telegrambot-golang/internal/services"
"github.com/tobigiwa/telegrambot-golang/internal/store"
"github.com/tobigiwa/telegrambot-golang/logging"
botBuild "github.com/tobigiwa/telegrambot-golang/bot"
tele "gopkg.in/telebot.v3"
)
func main() {
// DATABASE
conn, err := pgxpool.New(context.Background(), getDatabaseURL())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
db := store.NewRespository(conn)
err = db.Migrate()
if err != nil {
log.Fatal(err)
}
// LOGGER
logger, err := logging.NewLogger()
if err != nil {
log.Fatal(err)
}
// BOT
pref := tele.Settings{
Token: getBotToken(),
}
bot, err := tele.NewBot(pref)
if err != nil {
log.Fatal(err)
}
// APP
app := botBuild.Application{
Bot: bot,
Storage: db,
Logger: logger,
}
// Other setups
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
dir := cwd + "/assets"
err = os.MkdirAll(dir, 0755)
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(dir)
app.Logger.WriteToStandarOutput(cwd)
app.Logger.WriteToStandarOutput(fmt.Sprintf("Authorized on account... %s", app.Bot.Me.Username))
// keyboards
app.Bot.Handle(&botBuild.MotivationKeyboardBtn, app.MotivationKeyboardHandlerFunc)
app.Bot.Handle(&botBuild.TherapyKeyboardBtn, app.TherapyKeyboardHandleFunc)
app.Bot.Handle(&botBuild.RemindernKeyboardBtn, app.RemainderyKeyboardHandleFunc)
app.Bot.Handle(&botBuild.GameKeyboardBtn, app.GameKeyboardHandleFunc)
app.Bot.Handle(&botBuild.ReligionKeyboardBtn, app.ReligionKeyboardHandlerFunc)
app.Bot.Handle(&botBuild.BibleTextReligionMessageKeyboardBtn, app.GetBibleTextHandlerFunc)
app.Bot.Handle(&botBuild.AudioReligionMessageKeyboardBtn, app.GetAudioMessageHandlerFunc)
app.Bot.Handle(&botBuild.AudioAndTextReligionMessageKeyboardBtn, app.GetBothAudioAndTextReligionMessageHandlerFunc)
app.Bot.Handle(&botBuild.BackToStartKeyboardBtn, app.BackToStartHanlerFunc)
// inline keyboards
app.Bot.Handle(&botBuild.GetTodaysQouteInlineKeyboardBtn, app.GetTodaysQuoteFunc)
app.Bot.Handle(&botBuild.RandomQuotesInlineKeyboardBtn, app.GetRandomQuoteFunc)
app.Bot.Handle(&botBuild.ImageQoutesOnInlineKeyboardBtn, app.GetRandomQuoteImageFunc)
// any text
app.Bot.Handle(tele.OnText, app.StartHandlerFunc, app.CheckMemberShip)
// cron jobs
now := time.Now().UTC()
lagosTime := now.In(time.FixedZone("WAT", 3600))
s := gocron.NewScheduler(lagosTime.Location())
s.Every(1).Day().At("6:30").Do(app.ScheduledTaskText, app.Bot, services.ScrapeBibleText)
s.Every(1).Day().At("6:31").Do(app.ScheduledTaskMedia, app.Bot, botBuild.ResolveAudioMessge)
s.Every(1).Day().At("7:30").Do(app.ScheduledTaskText, app.Bot, services.GetTodaysQuote)
s.Every(1).Day().At("8:30").Do(app.ScheduledTaskMedia, app.Bot, botBuild.ResolveImageMessage)
s.Every(1).Day().At("20:00").Do(app.ScheduledTaskText, app.Bot, services.GetRandomQuote)
s.StartAsync()
// // Create a new Webhook object.
// wh := &tele.Webhook{
// Listen: ":8081",
// Endpoint: &tele.WebhookEndpoint{
// PublicURL: "https://271a-102-89-22-111.ngrok-free.app",
// },
// }
// // Set the webhook.
// err = app.Bot.SetWebhook(wh)
// if err != nil {
// log.Fatal(err)
// }
// if err := http.ListenAndServe(":8081", nil); err != nil {
// fmt.Println("at server")
// log.Fatal(err)
// }
app.Bot.Start()
}
func getBotToken() (token string) {
token, ok := os.LookupEnv("BOT_TOKEN")
if !ok || token == "" {
log.Fatal("No Token")
}
return token
}
func getDatabaseURL() (databaseURL string) {
databaseURL, ok := os.LookupEnv("DATABASE_URL")
if !ok || databaseURL == "" {
log.Fatal("No databaseURL")
}
return databaseURL
}