-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (87 loc) · 2.33 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
package main
import (
"encoding/json"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"os"
"strings"
)
func main() {
createbot()
}
//Creating the bot
func createbot() {
bot, err := tgbotapi.NewBotAPI(os.Getenv("API_TOKEN"))
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("Hello, I am " + bot.Self.FirstName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
fmt.Println(err.Error())
}
getUpdates(bot, updates)
}
//Waiting for updates
func getUpdates(bot *tgbotapi.BotAPI, updates tgbotapi.UpdatesChannel) {
for update := range updates {
go handleUpdate(bot, update)
}
}
func sendImage(bot *tgbotapi.BotAPI, d data, ChatID int64) {
req := newPhotoRequest(d)
res, err := getResponse(req)
if err != nil {
var msg tgbotapi.Chattable
if err.Error() == "couldn't find a image" {
msg = tgbotapi.NewMessage(ChatID, "Couldn't Find A Image For "+d.Query)
} else {
msg = tgbotapi.NewMessage(ChatID, strings.Title(err.Error()))
}
bot.Send(msg)
return
}
if len(res) == 0 {
msg := tgbotapi.NewMessage(ChatID, "Couldn't Find A Image For "+d.Query)
bot.Send(msg)
return
}
document := tgbotapi.NewDocumentShare(ChatID, res[0].Urls.Full)
document.Caption = "By " + res[0].User.FirstName + " " + res[0].User.LastName + " On Unsplash\n" + res[0].User.Links.Html
document.ReplyMarkup = newInlineKeyboard(data{Query: d.Query, Random: d.Random})
_, err = bot.Send(document)
if err != nil {
panic(err.Error())
}
}
//Handling Updates
func handleUpdate(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
var d data
if update.CallbackQuery != nil {
_ = json.Unmarshal([]byte(update.CallbackQuery.Data), &d)
sendImage(bot, d, update.CallbackQuery.Message.Chat.ID)
newCallbackConfig := tgbotapi.NewCallback(update.CallbackQuery.ID, "")
_, _ = bot.AnswerCallbackQuery(newCallbackConfig)
return
}
if update.Message == nil || update.Message.Text == "" {
return
}
if update.Message.Chat.IsPrivate() {
if update.Message.IsCommand() {
switch update.Message.Command() {
case "random":
d.Random = true
case "start":
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Welcome to "+bot.Self.FirstName+" "+bot.Self.LastName)
bot.Send(msg)
return
}
} else {
d.Query = update.Message.Text
}
sendImage(bot, d, update.Message.Chat.ID)
}
}