add server
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
38a21e4568
commit
ccd55a6702
|
@ -11,7 +11,7 @@ func NewConfig() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
MongoURL: "mongodb://mongo:o6bbyog3DHG0GYdu@158.160.11.219:27027",
|
MongoURL: "mongodb://mongo:o6bbyog3DHG0GYdu@158.160.11.219:27027",
|
||||||
DBName: "valera",
|
DBName: "valera",
|
||||||
ChatsCollectionName: "users",
|
ChatsCollectionName: "chats",
|
||||||
WorkoutsCollectionName: "workouts",
|
WorkoutsCollectionName: "workouts",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
35
db/db.go
35
db/db.go
|
@ -81,6 +81,9 @@ func AddChat(chatID int64) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddWorkout(chatID int64, workout *Workout) error {
|
func AddWorkout(chatID int64, workout *Workout) error {
|
||||||
|
if workout.Count <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if err := AddChat(chatID); err != nil {
|
if err := AddChat(chatID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -104,7 +107,7 @@ func AddWorkout(chatID int64, workout *Workout) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserInfo(chatID int64) (*ChatInfoDTO, error) {
|
func GetChatInfo(chatID int64) (*ChatInfoDTO, error) {
|
||||||
if err := AddChat(chatID); err != nil {
|
if err := AddChat(chatID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -124,7 +127,7 @@ func GetUserInfo(chatID int64) (*ChatInfoDTO, error) {
|
||||||
return chatInfoDTO, err
|
return chatInfoDTO, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetStatusInUser(chatID int64, status UserState) error {
|
func SetStatusToChat(chatID int64, status UserState) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
@ -181,3 +184,31 @@ func GetStat(chatID int64) (map[string]int, error) {
|
||||||
}
|
}
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAllChats() ([]int64, error) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI(cfg.MongoURL))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
collection := client.Database(cfg.DBName).Collection(cfg.ChatsCollectionName)
|
||||||
|
cursor, err := collection.Find(ctx,bson.M{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var res []int64
|
||||||
|
for cursor.Next(context.Background()) {
|
||||||
|
var result ChatInfoDTO
|
||||||
|
if err := cursor.Decode(&result); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
res = append(res, result.ChatID)
|
||||||
|
}
|
||||||
|
if err := cursor.Err(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
69
main.go
69
main.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"github.com/umputun/go-flags"
|
"github.com/umputun/go-flags"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -70,6 +71,28 @@ func run() {
|
||||||
panic(err)
|
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)
|
log.Println("Run", opts.Name)
|
||||||
|
|
||||||
for update := range updates {
|
for update := range updates {
|
||||||
|
@ -82,7 +105,7 @@ func run() {
|
||||||
chatID := update.Message.Chat.ID
|
chatID := update.Message.Chat.ID
|
||||||
username := update.Message.From.UserName
|
username := update.Message.From.UserName
|
||||||
|
|
||||||
userInfoDTO, err := db.GetUserInfo(chatID)
|
userInfoDTO, err := db.GetChatInfo(chatID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
continue
|
continue
|
||||||
|
@ -97,10 +120,14 @@ func run() {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
continue
|
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)
|
msg.ReplyMarkup = tgbot.NewHideKeyboard(false)
|
||||||
_, _ = bot.Send(msg)
|
_, _ = bot.Send(msg)
|
||||||
if err := db.SetStatusInUser(chatID, db.UserStateNone); err != nil {
|
if err := db.SetStatusToChat(chatID, db.UserStateNone); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
|
@ -110,7 +137,7 @@ func run() {
|
||||||
switch command {
|
switch command {
|
||||||
case commands.Start:
|
case commands.Start:
|
||||||
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здорова, я Валера (%s), твой тренер (%d).", version, chatID)))
|
_, _ = 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)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
case commands.Help:
|
case commands.Help:
|
||||||
|
@ -118,21 +145,7 @@ func run() {
|
||||||
case commands.Ping:
|
case commands.Ping:
|
||||||
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
|
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
|
||||||
case commands.Go:
|
case commands.Go:
|
||||||
msg := tgbot.NewMessage(chatID, "Давай немного разомнемся, отжимания, отпишись сколько раз ты выполнил упражнение")
|
sendGoToChat(bot, 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case commands.Stat:
|
case commands.Stat:
|
||||||
stat, err := db.GetStat(chatID)
|
stat, err := db.GetStat(chatID)
|
||||||
if err != nil {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue