-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
114 lines (99 loc) · 2.73 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
package main
import (
"fmt"
"log"
"os"
"reflect"
"strings"
c "go_bot/common"
db "go_bot/database"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func main() {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TG_API_KEY"))
if err != nil {
log.Panic(err)
}
bot.Debug = false
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Panic(err)
}
for update := range updates {
if update.Message == nil {
continue
}
// Start command
if strings.HasPrefix(update.Message.Command(), "start") {
userInsertToDb := db.User{
UserID: update.Message.From.ID,
FirstName: update.Message.From.FirstName,
LastName: update.Message.From.LastName,
UserName: update.Message.From.UserName,
}
db.Insert(userInsertToDb)
text := fmt.Sprintf("Hi %v, I am XurBot and I'll show you Xur's location. Just use /xur_location command.",
update.Message.From.FirstName)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, text)
if _, err := bot.Send(msg); err != nil {
log.Println(err)
}
}
// item_ command
if strings.HasPrefix(update.Message.Command(), "item_") {
hash := c.HashRegexp(update.Message.Command())
item, errHash := c.ParseHashDataOneItem(hash)
if errHash != nil {
log.Println(errHash)
}
if reflect.ValueOf(item).IsZero() {
imp := tgbotapi.NewMessage(update.Message.Chat.ID, "")
imp.Text = "Incorrect command"
if _, err := bot.Send(imp); err != nil {
log.Println(err)
}
} else {
imp := tgbotapi.NewPhotoUpload(update.Message.Chat.ID, nil)
text := fmt.Sprintf("<b>%v</b>\n"+
"<i>%v</i>\n\n"+
"%v\n\n<a href=\"https://www.light.gg/db/items/%v\">More info</a>",
item.Name,
item.ItemTypeAndTierDisplayName,
item.FlavorText,
item.Hash,
)
imp.FileID = "https://www.bungie.net" + item.Screenshot
imp.Caption = text
imp.ParseMode = "HTML"
imp.UseExisting = true
if _, err := bot.Send(imp); err != nil {
log.Println(err)
}
}
}
// xur_location command
if strings.HasPrefix(update.Message.Command(), "xur_location") {
xur := c.GetXurData()
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
msg.ParseMode = "HTML"
if xur == nil {
text_msg := fmt.Sprintln("Xur isn't here")
msg.Text = text_msg
} else {
text_msg := []string{fmt.Sprintf("I am on <b>%v</b> in <b>%v</b> and I have something for you:\n",
xur.LocationName,
xur.PlaceName),
}
xurItems := c.ParseHashesData()
text_msg = append(text_msg, xurItems...)
msg.Text = strings.Join(text_msg[:], "\n")
}
if _, err := bot.Send(msg); err != nil {
log.Println(err)
}
}
}
}