add server
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-03-11 19:38:01 +07:00
parent 38a21e4568
commit ccd55a6702
3 changed files with 84 additions and 22 deletions
+50 -19
View File
@@ -7,6 +7,7 @@ import (
"github.com/umputun/go-flags"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
@@ -70,6 +71,28 @@ func run() {
panic(err)
}
go func() {
http.HandleFunc("/go", func(w http.ResponseWriter, r *http.Request) {
chats, err := db.GetAllChats()
if err != nil {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
_, _ = fmt.Fprintf(w, `{"result":"error"}`)
return
}
for _, chatID := range chats {
sendGoToChat(bot, chatID)
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, `{"result":"ok"}`)
})
port := ":10002"
log.Println("Server is start up! port", port)
log.Fatal(http.ListenAndServe(port, nil))
}()
log.Println("Run", opts.Name)
for update := range updates {
@@ -82,7 +105,7 @@ func run() {
chatID := update.Message.Chat.ID
username := update.Message.From.UserName
userInfoDTO, err := db.GetUserInfo(chatID)
userInfoDTO, err := db.GetChatInfo(chatID)
if err != nil {
log.Println(err)
continue
@@ -97,10 +120,14 @@ func run() {
log.Println(err)
continue
}
msg := tgbot.NewMessage(chatID, fmt.Sprintf("Отлично, %s, записал.", text))
msgText := fmt.Sprintf("Отлично, %s, записал.", text)
if count <= 0 {
msgText = "Плохо, хочешь быть толстым и не красивым?"
}
msg := tgbot.NewMessage(chatID, msgText)
msg.ReplyMarkup = tgbot.NewHideKeyboard(false)
_, _ = bot.Send(msg)
if err := db.SetStatusInUser(chatID, db.UserStateNone); err != nil {
if err := db.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err)
}
continue
@@ -110,7 +137,7 @@ func run() {
switch command {
case commands.Start:
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID)))
if err := db.SetStatusInUser(chatID, db.UserStateNone); err != nil {
if err := db.SetStatusToChat(chatID, db.UserStateNone); err != nil {
log.Println(err)
}
case commands.Help:
@@ -118,21 +145,7 @@ func run() {
case commands.Ping:
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
case commands.Go:
msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, отжимания, отпишись сколько раз ты выполнил упражнение")
msg.ReplyMarkup = tgbot.NewReplyKeyboard(
tgbot.NewKeyboardButtonRow(
tgbot.NewKeyboardButton("1"),
tgbot.NewKeyboardButton("2"),
tgbot.NewKeyboardButton("3"),
tgbot.NewKeyboardButton("5"),
tgbot.NewKeyboardButton("8"),
),
)
if _, err = bot.Send(msg); err == nil {
if err := db.SetStatusInUser(chatID, db.UserStateGo); err != nil {
log.Println(err)
}
}
sendGoToChat(bot, chatID)
case commands.Stat:
stat, err := db.GetStat(chatID)
if err != nil {
@@ -147,3 +160,21 @@ func run() {
}
}
}
func sendGoToChat(bot *tgbot.BotAPI, chatID int64) {
msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, отжимания, отпишись сколько раз ты выполнил упражнение")
msg.ReplyMarkup = tgbot.NewReplyKeyboard(
tgbot.NewKeyboardButtonRow(
tgbot.NewKeyboardButton("1"),
tgbot.NewKeyboardButton("2"),
tgbot.NewKeyboardButton("3"),
tgbot.NewKeyboardButton("5"),
tgbot.NewKeyboardButton("8"),
),
)
if _, err := bot.Send(msg); err == nil {
if err := db.SetStatusToChat(chatID, db.UserStateGo); err != nil {
log.Println(err)
}
}
}