add server method send_today_lessons_to_chat
This commit is contained in:
parent
bde8e7dae0
commit
c3fef99fc3
|
@ -33,6 +33,8 @@ steps:
|
||||||
from_secret: bot_token
|
from_secret: bot_token
|
||||||
TOKEN_WEATHER:
|
TOKEN_WEATHER:
|
||||||
from_secret: bot_token_weather
|
from_secret: bot_token_weather
|
||||||
|
CHAT_ID:
|
||||||
|
from_secret: bot_chat_id
|
||||||
PASSWORD:
|
PASSWORD:
|
||||||
from_secret: ssh_pass
|
from_secret: ssh_pass
|
||||||
settings:
|
settings:
|
||||||
|
@ -44,12 +46,13 @@ steps:
|
||||||
from_secret: ssh_pass
|
from_secret: ssh_pass
|
||||||
port:
|
port:
|
||||||
from_secret: ssh_port
|
from_secret: ssh_port
|
||||||
envs: [ TOKEN,TOKEN_WEATHER,PASSWORD ]
|
envs: [ TOKEN,TOKEN_WEATHER,CHAT_ID,PASSWORD ]
|
||||||
command_timeout: 10s
|
command_timeout: 10s
|
||||||
script:
|
script:
|
||||||
- cd bots/verochka_tg_bot
|
- cd bots/verochka_tg_bot
|
||||||
- echo $${TOKEN} > token.txt
|
- echo $${TOKEN} > token.txt
|
||||||
- echo $${TOKEN_WEATHER} > token_weather.txt
|
- echo $${TOKEN_WEATHER} > token_weather.txt
|
||||||
|
- echo $${CHAT_ID} > token_weather.txt
|
||||||
- echo $${PASSWORD} | sudo -S systemctl restart verochka_tg_bot
|
- echo $${PASSWORD} | sudo -S systemctl restart verochka_tg_bot
|
||||||
|
|
||||||
trigger:
|
trigger:
|
||||||
|
|
|
@ -5,3 +5,4 @@
|
||||||
|
|
||||||
token.txt
|
token.txt
|
||||||
token_weather.txt
|
token_weather.txt
|
||||||
|
chat_id.txt
|
||||||
|
|
69
main.go
69
main.go
|
@ -8,6 +8,7 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"student_bot/commands"
|
"student_bot/commands"
|
||||||
"student_bot/date"
|
"student_bot/date"
|
||||||
|
@ -22,9 +23,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Opts struct {
|
type Opts struct {
|
||||||
Token string `short:"t" long:"token" description:"Telegram api token"`
|
Token string `short:"t" long:"token" description:"Telegram api token"`
|
||||||
Name string `short:"n" long:"name" description:"Telegram bot name" default:"@student_verochka_bot"`
|
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"`
|
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
|
var sendMessage = false
|
||||||
|
@ -37,6 +39,7 @@ func readToken() (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
str := string(b)
|
str := string(b)
|
||||||
|
str = strings.ReplaceAll(str, "\n", "")
|
||||||
return str, nil
|
return str, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,9 +49,20 @@ func readWeatherToken() (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
str := string(b)
|
str := string(b)
|
||||||
|
str = strings.ReplaceAll(str, "\n", "")
|
||||||
return str, nil
|
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() {
|
func main() {
|
||||||
run()
|
run()
|
||||||
}
|
}
|
||||||
|
@ -68,7 +82,7 @@ func run() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
opts.Token = strings.ReplaceAll(token, "\n", "")
|
opts.Token = token
|
||||||
}
|
}
|
||||||
fmt.Println(opts.Token)
|
fmt.Println(opts.Token)
|
||||||
|
|
||||||
|
@ -77,10 +91,19 @@ func run() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
opts.Key = strings.ReplaceAll(token, "\n", "")
|
opts.Key = token
|
||||||
}
|
}
|
||||||
fmt.Println(opts.Key)
|
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())
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
imageService, err := new_year_service.NewNewYearService()
|
imageService, err := new_year_service.NewNewYearService()
|
||||||
|
@ -104,6 +127,7 @@ func run() {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
http.HandleFunc("/send_today_lessons_to_chat", func(w http.ResponseWriter, r *http.Request) {
|
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.Header().Add("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, _ = fmt.Fprintf(w, `{"result":"ok"}`)
|
_, _ = fmt.Fprintf(w, `{"result":"ok"}`)
|
||||||
|
@ -124,11 +148,11 @@ func run() {
|
||||||
}
|
}
|
||||||
|
|
||||||
text := update.Message.Text
|
text := update.Message.Text
|
||||||
chatId := update.Message.Chat.ID
|
chatID := update.Message.Chat.ID
|
||||||
|
|
||||||
if strings.Contains(strings.ToLower(text), "спасибо") {
|
if strings.Contains(strings.ToLower(text), "спасибо") {
|
||||||
if sendMessage {
|
if sendMessage {
|
||||||
_, _ = bot.Send(tgbot.NewMessage(chatId, messages.ThanksMessage()))
|
_, _ = bot.Send(tgbot.NewMessage(chatID, messages.ThanksMessage()))
|
||||||
sendMessage = false
|
sendMessage = false
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
|
@ -139,23 +163,19 @@ func run() {
|
||||||
switch command {
|
switch command {
|
||||||
|
|
||||||
case commands.Start:
|
case commands.Start:
|
||||||
send(bot, tgbot.NewMessage(chatId, messages.StartMessage()))
|
send(bot, tgbot.NewMessage(chatID, messages.StartMessage()))
|
||||||
|
|
||||||
case commands.Help:
|
case commands.Help:
|
||||||
send(bot, tgbot.NewMessage(chatId, messages.HelpMessage(chatId)))
|
send(bot, tgbot.NewMessage(chatID, messages.HelpMessage(chatID)))
|
||||||
|
|
||||||
case commands.Ping:
|
case commands.Ping:
|
||||||
send(bot, tgbot.NewMessage(chatId, messages.PongMessage()))
|
send(bot, tgbot.NewMessage(chatID, messages.PongMessage()))
|
||||||
|
|
||||||
case commands.TodayLessons:
|
case commands.TodayLessons:
|
||||||
send(bot, tgbot.NewMessage(chatId, messages.LessonsMessage(
|
sendTodayLessonsToChat(bot, chatID, "Сегодня, "+update.Message.From.FirstName+", эти пары:")
|
||||||
parser.ParseByDay(date.Today()),
|
|
||||||
"Сегодня, "+update.Message.From.FirstName+", эти пары:",
|
|
||||||
"Сегодня пар нет",
|
|
||||||
)))
|
|
||||||
|
|
||||||
case commands.TomorrowLessons:
|
case commands.TomorrowLessons:
|
||||||
send(bot, tgbot.NewMessage(chatId, messages.LessonsMessage(
|
send(bot, tgbot.NewMessage(chatID, messages.LessonsMessage(
|
||||||
parser.ParseByDay(date.Today()+1),
|
parser.ParseByDay(date.Today()+1),
|
||||||
"Завтра, "+update.Message.From.FirstName+", эти пары:",
|
"Завтра, "+update.Message.From.FirstName+", эти пары:",
|
||||||
"Завтра пар нет",
|
"Завтра пар нет",
|
||||||
|
@ -166,12 +186,12 @@ func run() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
send(bot, tgbot.NewMessage(chatId, messages.WeatherMessage(w)))
|
send(bot, tgbot.NewMessage(chatID, messages.WeatherMessage(w)))
|
||||||
|
|
||||||
case commands.NewYear:
|
case commands.NewYear:
|
||||||
url := imageService.GetRandomImageURL()
|
url := imageService.GetRandomImageURL()
|
||||||
message := imageService.GetRandomMessage()
|
message := imageService.GetRandomMessage()
|
||||||
msg := tgbot.NewPhotoShare(chatId, url)
|
msg := tgbot.NewPhotoShare(chatID, url)
|
||||||
msg.Caption = messages.NewYearMessage(message)
|
msg.Caption = messages.NewYearMessage(message)
|
||||||
send(bot, msg)
|
send(bot, msg)
|
||||||
|
|
||||||
|
@ -184,3 +204,16 @@ func send(bot *tgbot.BotAPI, msg tgbot.Chattable) {
|
||||||
_, _ = bot.Send(msg)
|
_, _ = bot.Send(msg)
|
||||||
sendMessage = true
|
sendMessage = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func sendTodayLessonsToChat(bot *tgbot.BotAPI, chatID int64, prefix string) {
|
||||||
|
send(
|
||||||
|
bot,
|
||||||
|
tgbot.NewMessage(chatID,
|
||||||
|
messages.LessonsMessage(
|
||||||
|
parser.ParseByDay(date.Today()),
|
||||||
|
prefix,
|
||||||
|
"Сегодня пар нет",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue