Используем более короткую форму для фатальных ошибок, более подробное логгирование
This commit is contained in:
30
main.go
30
main.go
@@ -15,29 +15,25 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
cfg, err := ini.Load("galichbot.ini")
|
cfg, err := ini.Load("galichbot.ini")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Fatalln(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
token := cfg.Section("").Key("BotApiKey").String()
|
token := cfg.Section("").Key("BotApiKey").String()
|
||||||
victim, err := cfg.Section("").Key("VictimID").Int64()
|
victim, err := cfg.Section("").Key("VictimID").Int64()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Fatalln(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fileName := "phrases.txt"
|
fileName := "phrases.txt"
|
||||||
fileBytes, err := ioutil.ReadFile(fileName)
|
fileBytes, err := ioutil.ReadFile(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Fatalln(err)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
speeches := strings.Split(string(fileBytes), "--")
|
speeches := strings.Split(string(fileBytes), "--")
|
||||||
bot, err := micha.NewBot(token)
|
bot, err := micha.NewBot(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Fatalln(err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go bot.Start()
|
go bot.Start()
|
||||||
@@ -45,10 +41,26 @@ func main() {
|
|||||||
|
|
||||||
for update := range bot.Updates() {
|
for update := range bot.Updates() {
|
||||||
if update.Message != nil {
|
if update.Message != nil {
|
||||||
|
log.Printf(
|
||||||
|
"%s %s %s (%d) from %s (%d) says: $s",
|
||||||
|
update.Message.From.Username,
|
||||||
|
update.Message.From.FirstName,
|
||||||
|
update.Message.From.LastName,
|
||||||
|
update.Message.From.ID,
|
||||||
|
update.Message.Chat.Title,
|
||||||
|
update.Message.Chat.ID,
|
||||||
|
update.Message.Text
|
||||||
|
)
|
||||||
|
|
||||||
if update.Message.From.ID == victim {
|
if update.Message.From.ID == victim {
|
||||||
n := rand.Int() % len(speeches)
|
n := rand.Int() % len(speeches)
|
||||||
var options = micha.SendMessageOptions{ReplyToMessageID: update.Message.MessageID}
|
var options = micha.SendMessageOptions{ReplyToMessageID: update.Message.MessageID}
|
||||||
_, _ = bot.SendMessage(update.Message.Chat.ID, speeches[n], &options)
|
_, err = bot.SendMessage(update.Message.Chat.ID, speeches[n], &options)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
} else {
|
||||||
|
log.Printf("Bot answers: %s", speeches[n])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user