-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (92 loc) · 2.4 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
package main
import (
"fmt"
"log"
"os"
"strings"
"duarteocarmo/ambrosio/modes"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
const (
PhotoMode = "photo"
AssistantMode = "assistant"
Timeout = 60
)
func createBot() (*tgbotapi.BotAPI, error) {
m := os.Getenv("MODE")
t := ""
d := false
switch m {
case "DEV":
t = os.Getenv("TELEGRAM_APITOKEN_DEV")
d = true
log.Println("Running in DEV mode")
case "PROD":
t = os.Getenv("TELEGRAM_APITOKEN_PROD")
d = false
log.Println("Running in PROD mode")
default:
log.Println("No mode specified. Exiting...")
return nil, fmt.Errorf("no mode specified")
}
bot, err := tgbotapi.NewBotAPI(t)
if err != nil {
return nil, fmt.Errorf("error creating bot: %v", err)
}
bot.Debug = d
log.Printf("Authorized on account %s", bot.Self.UserName)
log.Printf("Bot is running")
return bot, nil
}
func main() {
bot, err := createBot()
if err != nil {
log.Panicf("Error creating bot: %v", err)
}
u := tgbotapi.NewUpdate(0)
u.Timeout = Timeout
updates := bot.GetUpdatesChan(u)
authorizedUser := os.Getenv("TELEGRAM_USERNAME")
availableModes := []string{PhotoMode, AssistantMode}
helpMsg := "I don't know that command. Available commands are: \n/" + strings.Join(availableModes, "\n/")
for update := range updates {
if update.Message == nil {
continue
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
if update.SentFrom().UserName != authorizedUser {
msg.Text = "Sorry, you are not authorized to use this bot"
bot.Send(msg)
log.Printf("Detected unauthorized user %s", update.SentFrom().UserName)
continue
}
if !update.Message.IsCommand() {
msg.Text = helpMsg
bot.Send(msg)
continue
}
switch update.Message.Command() {
case PhotoMode, strings.ToLower(PhotoMode)[0:1]:
err := modes.PhotoMode(update, updates, bot)
if err != nil {
log.Printf("Error in photo mode: %v", err)
msg.Text = fmt.Sprintf("Error in photo mode: %v", err)
bot.Send(msg)
}
continue
case AssistantMode, strings.ToLower(AssistantMode)[0:1]:
err := modes.AssistantMode(update, updates, bot)
if err != nil {
log.Printf("Error in assistant mode: %v", err)
msg.Text = fmt.Sprintf("Error in assistant mode: %v", err)
bot.Send(msg)
}
continue
default:
msg.Text = helpMsg
}
if _, err := bot.Send(msg); err != nil {
log.Panicf("Error sending message: %v", err)
}
}
}