verochka/main.go

187 lines
4.2 KiB
Go
Raw Normal View History

2022-03-05 18:02:55 +00:00
package main
import (
"errors"
2022-04-24 17:46:48 +00:00
"fmt"
"io/ioutil"
2022-03-05 18:02:55 +00:00
"log"
"math/rand"
2022-09-04 15:32:58 +00:00
"net/http"
2022-03-05 18:02:55 +00:00
"os"
"strings"
"student_bot/commands"
"student_bot/date"
"student_bot/messages"
"student_bot/new_year_service"
"student_bot/parser"
"time"
weather "github.com/3crabs/go-yandex-weather-api"
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/umputun/go-flags"
)
type Opts struct {
Token string `short:"t" long:"token" description:"Telegram api token"`
Name string `short:"n" long:"name" description:"Telegram bot name" default:"@student_verochka_bot"`
2022-04-24 17:46:48 +00:00
Key string `short:"k" long:"key" description:"Yandex weather API key"`
2022-03-05 18:02:55 +00:00
}
var sendMessage = false
var opts Opts
2022-04-24 17:46:48 +00:00
func readToken() (string, error) {
b, err := ioutil.ReadFile("token.txt")
if err != nil {
return "", err
}
str := string(b)
return str, nil
}
func readWeatherToken() (string, error) {
b, err := ioutil.ReadFile("token_weather.txt")
if err != nil {
return "", err
}
str := string(b)
return str, nil
}
2022-03-05 18:02:55 +00:00
func main() {
run()
}
func run() {
p := flags.NewParser(&opts, flags.PrintErrors|flags.PassDoubleDash|flags.HelpFlag)
p.SubcommandsOptional = true
if _, err := p.Parse(); err != nil {
if err.(*flags.Error).Type != flags.ErrHelp {
log.Println(errors.New("[ERROR] cli error: " + err.Error()))
}
os.Exit(2)
}
2022-04-24 17:46:48 +00:00
if opts.Token == "" {
token, err := readToken()
if err != nil {
panic(err)
}
opts.Token = strings.ReplaceAll(token, "\n", "")
}
fmt.Println(opts.Token)
if opts.Key == "" {
token, err := readWeatherToken()
if err != nil {
panic(err)
}
opts.Key = strings.ReplaceAll(token, "\n", "")
}
fmt.Println(opts.Key)
rand.Seed(time.Now().UnixNano())
2022-03-05 18:02:55 +00:00
imageService, err := new_year_service.NewNewYearService()
if err != nil {
log.Panicln(err)
}
imageService = new_year_service.NewNewYearServiceLogWrapper(imageService)
bot, err := tgbot.NewBotAPI(opts.Token)
if err != nil {
log.Panicln(err)
}
u := tgbot.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
log.Panicln(err)
}
2022-09-04 15:32:58 +00:00
go func() {
http.HandleFunc("/send_today_lessons_to_chat", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, `{"result":"ok"}`)
})
port := ":10001"
log.Println("Server is start up! port", port)
log.Fatal(http.ListenAndServe(port, nil))
}()
2022-03-05 18:02:55 +00:00
log.Println("Bot is start up!")
for update := range updates {
// empty message
if update.Message == nil {
continue
}
text := update.Message.Text
chatId := update.Message.Chat.ID
if strings.Contains(strings.ToLower(text), "спасибо") {
if sendMessage {
_, _ = bot.Send(tgbot.NewMessage(chatId, messages.ThanksMessage()))
sendMessage = false
}
continue
}
sendMessage = false
command := commands.Command(strings.Replace(text, opts.Name, "", 1))
switch command {
case commands.Start:
send(bot, tgbot.NewMessage(chatId, messages.StartMessage()))
case commands.Help:
2022-09-04 15:32:58 +00:00
send(bot, tgbot.NewMessage(chatId, messages.HelpMessage(chatId)))
2022-03-05 18:02:55 +00:00
case commands.Ping:
send(bot, tgbot.NewMessage(chatId, messages.PongMessage()))
case commands.TodayLessons:
send(bot, tgbot.NewMessage(chatId, messages.LessonsMessage(
parser.ParseByDay(date.Today()),
"Сегодня, "+update.Message.From.FirstName+", эти пары:",
"Сегодня пар нет",
)))
case commands.TomorrowLessons:
send(bot, tgbot.NewMessage(chatId, messages.LessonsMessage(
parser.ParseByDay(date.Today()+1),
"Завтра, "+update.Message.From.FirstName+", эти пары:",
"Завтра пар нет",
)))
case commands.Weather:
w, err := weather.GetWeatherWithCache(opts.Key, 53.346853, 83.777012, time.Hour)
if err != nil {
continue
}
send(bot, tgbot.NewMessage(chatId, messages.WeatherMessage(w)))
case commands.NewYear:
url := imageService.GetRandomImageURL()
message := imageService.GetRandomMessage()
msg := tgbot.NewPhotoShare(chatId, url)
msg.Caption = messages.NewYearMessage(message)
send(bot, msg)
default:
}
}
}
func send(bot *tgbot.BotAPI, msg tgbot.Chattable) {
_, _ = bot.Send(msg)
sendMessage = true
}