/
masterbot.go
62 lines (54 loc) · 1.38 KB
/
masterbot.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
package main
import (
"io/ioutil"
"log"
"math/rand"
"os"
"strings"
"time"
"github.com/tucnak/telebot"
)
const (
BotApiKey = "API_KEY" // "Telegram API Key"
)
var (
logger = log.New(os.Stderr, "[main] ", log.LstdFlags)
)
func processGeneralMessage(str string, dict []string) string {
return dict[rand.Intn(len(dict))]
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
dictData, err := ioutil.ReadFile("dict.txt")
if err != nil {
logger.Fatal(err)
}
dict := strings.Split(string(dictData), "\n")
SecretAPIKey := os.Getenv(BotApiKey)
if SecretAPIKey == "" {
logger.Fatalf("env variable %s not specifited", BotApiKey)
}
bot, err := telebot.NewBot(SecretAPIKey)
if err != nil {
logger.Fatal(err)
}
messages := make(chan telebot.Message)
bot.Listen(messages, 1*time.Second)
sendOptions := telebot.SendOptions{
ReplyMarkup: telebot.ReplyMarkup{
ForceReply: true,
Selective: true,
CustomKeyboard: [][]string{
[]string{"Совет Гуру"},
},
},
}
for message := range messages {
logger.Printf("Req %s from user %s ", message.Text, message.Sender.FirstName)
if message.Text == "/start" {
bot.SendMessage(message.Chat, "Привет, я раскажу тебе как двигать тазом, "+message.Sender.FirstName+"!", &sendOptions)
} else {
bot.SendMessage(message.Chat, processGeneralMessage(message.Text, dict), &sendOptions)
}
}
}