add server method send_today_lessons_to_chat

This commit is contained in:
Владимир Фёдоров 2022-09-04 22:48:20 +07:00
parent bde8e7dae0
commit c3fef99fc3
3 changed files with 56 additions and 19 deletions

View File

@ -33,6 +33,8 @@ steps:
from_secret: bot_token
TOKEN_WEATHER:
from_secret: bot_token_weather
CHAT_ID:
from_secret: bot_chat_id
PASSWORD:
from_secret: ssh_pass
settings:
@ -44,12 +46,13 @@ steps:
from_secret: ssh_pass
port:
from_secret: ssh_port
envs: [ TOKEN,TOKEN_WEATHER,PASSWORD ]
envs: [ TOKEN,TOKEN_WEATHER,CHAT_ID,PASSWORD ]
command_timeout: 10s
script:
- cd bots/verochka_tg_bot
- echo $${TOKEN} > token.txt
- echo $${TOKEN_WEATHER} > token_weather.txt
- echo $${CHAT_ID} > token_weather.txt
- echo $${PASSWORD} | sudo -S systemctl restart verochka_tg_bot
trigger:

1
.gitignore vendored
View File

@ -5,3 +5,4 @@
token.txt
token_weather.txt
chat_id.txt

69
main.go
View File

@ -8,6 +8,7 @@ import (
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"student_bot/commands"
"student_bot/date"
@ -22,9 +23,10 @@ import (
)
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"`
Key string `short:"k" long:"key" description:"Yandex weather API key"`
Token string `short:"t" long:"token" description:"Telegram api token"`
Name string `short:"n" long:"name" description:"Telegram bot name" default:"@student_verochka_bot"`
Key string `short:"k" long:"key" description:"Yandex weather API key"`
ChatID int64 `short:"c" long:"chat_id" description:"Telegram chat id"`
}
var sendMessage = false
@ -37,6 +39,7 @@ func readToken() (string, error) {
return "", err
}
str := string(b)
str = strings.ReplaceAll(str, "\n", "")
return str, nil
}
@ -46,9 +49,20 @@ func readWeatherToken() (string, error) {
return "", err
}
str := string(b)
str = strings.ReplaceAll(str, "\n", "")
return str, nil
}
func readChatID() (int64, error) {
b, err := ioutil.ReadFile("chat_id.txt")
if err != nil {
return 0, err
}
str := string(b)
str = strings.ReplaceAll(str, "\n", "")
return strconv.ParseInt(str, 10, 64)
}
func main() {
run()
}
@ -68,7 +82,7 @@ func run() {
if err != nil {
panic(err)
}
opts.Token = strings.ReplaceAll(token, "\n", "")
opts.Token = token
}
fmt.Println(opts.Token)
@ -77,10 +91,19 @@ func run() {
if err != nil {
panic(err)
}
opts.Key = strings.ReplaceAll(token, "\n", "")
opts.Key = token
}
fmt.Println(opts.Key)
if opts.ChatID == 0 {
chatID, err := readChatID()
if err != nil {
panic(err)
}
opts.ChatID = chatID
}
fmt.Println(opts.ChatID)
rand.Seed(time.Now().UnixNano())
imageService, err := new_year_service.NewNewYearService()
@ -104,6 +127,7 @@ func run() {
go func() {
http.HandleFunc("/send_today_lessons_to_chat", func(w http.ResponseWriter, r *http.Request) {
sendTodayLessonsToChat(bot, opts.ChatID, "Сегодня эти пары:")
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, `{"result":"ok"}`)
@ -124,11 +148,11 @@ func run() {
}
text := update.Message.Text
chatId := update.Message.Chat.ID
chatID := update.Message.Chat.ID
if strings.Contains(strings.ToLower(text), "спасибо") {
if sendMessage {
_, _ = bot.Send(tgbot.NewMessage(chatId, messages.ThanksMessage()))
_, _ = bot.Send(tgbot.NewMessage(chatID, messages.ThanksMessage()))
sendMessage = false
}
continue
@ -139,23 +163,19 @@ func run() {
switch command {
case commands.Start:
send(bot, tgbot.NewMessage(chatId, messages.StartMessage()))
send(bot, tgbot.NewMessage(chatID, messages.StartMessage()))
case commands.Help:
send(bot, tgbot.NewMessage(chatId, messages.HelpMessage(chatId)))
send(bot, tgbot.NewMessage(chatID, messages.HelpMessage(chatID)))
case commands.Ping:
send(bot, tgbot.NewMessage(chatId, messages.PongMessage()))
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+", эти пары:",
"Сегодня пар нет",
)))
sendTodayLessonsToChat(bot, chatID, "Сегодня, "+update.Message.From.FirstName+", эти пары:")
case commands.TomorrowLessons:
send(bot, tgbot.NewMessage(chatId, messages.LessonsMessage(
send(bot, tgbot.NewMessage(chatID, messages.LessonsMessage(
parser.ParseByDay(date.Today()+1),
"Завтра, "+update.Message.From.FirstName+", эти пары:",
"Завтра пар нет",
@ -166,12 +186,12 @@ func run() {
if err != nil {
continue
}
send(bot, tgbot.NewMessage(chatId, messages.WeatherMessage(w)))
send(bot, tgbot.NewMessage(chatID, messages.WeatherMessage(w)))
case commands.NewYear:
url := imageService.GetRandomImageURL()
message := imageService.GetRandomMessage()
msg := tgbot.NewPhotoShare(chatId, url)
msg := tgbot.NewPhotoShare(chatID, url)
msg.Caption = messages.NewYearMessage(message)
send(bot, msg)
@ -184,3 +204,16 @@ func send(bot *tgbot.BotAPI, msg tgbot.Chattable) {
_, _ = bot.Send(msg)
sendMessage = true
}
func sendTodayLessonsToChat(bot *tgbot.BotAPI, chatID int64, prefix string) {
send(
bot,
tgbot.NewMessage(chatID,
messages.LessonsMessage(
parser.ParseByDay(date.Today()),
prefix,
"Сегодня пар нет",
),
),
)
}