-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (81 loc) · 2.16 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
// Simple bot that realize some dialog with animal
//
package main
import (
// "fmt"
"log"
"math/rand"
"strings"
"time"
"os"
"encoding/json"
"io/ioutil"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type Config struct {
ConfigDebug bool `json:"debug"`
}
var (
questions = []string{
"Гав", "Ваф", "Вуф", "Тяв", "Уф", "Врррррр", "Грррррр",
}
dog = "\xF0\x9F\x90\xB6"
standardResponses = map[string][]string{
"спасибо": {"Пожалуйста!", "Рад помочь!", "Не за что!"},
"хорошо": {"Отлично!", "Замечательно!", "Прекрасно!"},
"окей": {"Понял вас!", "Хорошо!", "Ясно!"},
}
)
func main() {
var reply string
// Config block
file, err := ioutil.ReadFile("config.json")
if err != nil {
log.Fatal("Can't read config.json file", err)
}
var config Config
err = json.Unmarshal(file, &config)
if err != nil {
log.Fatal("Can't unmarshal config.json", err)
}
// Get SECRET
secretToken := os.Getenv("ANIMALOT_SECRET_TOKEN")
if secretToken == "" {
log.Println("ANIMALOT_SECRET_TOKEN not set")
return
}
// Init bot
bot, err := tgbotapi.NewBotAPI(secretToken)
if err != nil {
log.Panic("Panic, problem with connect to telegram API: ", err)
}
bot.Debug = config.ConfigDebug
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil {
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
// Check for standard responses
lowercaseText := strings.ToLower(update.Message.Text)
responses, ok := standardResponses[lowercaseText]
if ok {
reply = responses[rand.Intn(len(responses))]
} else {
// Ask a random question
reply = dog + questions[rand.Intn(len(questions))]
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, reply)
msg.ReplyToMessageID = update.Message.MessageID
_, err := bot.Send(msg)
if err != nil {
log.Println("Some error with send: ", err)
}
}
}
func init() {
rand.Seed(time.Now().UnixNano())
}