Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Commit

Permalink
🔖 Releasing v1.2
Browse files Browse the repository at this point in the history
With buttons-menu, improved searching and 'delPack' command!
  • Loading branch information
toby3d committed Jan 19, 2018
1 parent 6ade04c commit 7443022
Show file tree
Hide file tree
Showing 20 changed files with 111 additions and 144 deletions.
31 changes: 31 additions & 0 deletions actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
log "github.com/kirillDanshin/dlog"
tg "github.com/toby3d/telegram"
)

// actions function check Message update on commands, sended stickers or other user stuff
func actions(msg *tg.Message) {
state, err := dbGetUserState(msg.From.ID)
errCheck(err)

log.Ln("state:", state)
switch state {
case stateAddSticker:
actionAdd(msg, false)
case stateAddPack:
actionAdd(msg, true)
case stateDeleteSticker:
actionDelete(msg, false)
case stateDeletePack:
actionDelete(msg, true)
case stateReset:
actionReset(msg)
default:
err = dbChangeUserState(msg.From.ID, stateNone)
errCheck(err)

actionError(msg)
}
}
12 changes: 3 additions & 9 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ func actionAdd(msg *tg.Message, pack bool) {

if !pack {
var exist bool
sticker := msg.Sticker
exist, err = dbAddSticker(
msg.From.ID,
msg.Sticker.SetName,
msg.Sticker.FileID,
msg.Sticker.Emoji,
msg.From.ID, sticker.SetName, sticker.FileID, sticker.Emoji,
)
errCheck(err)

Expand Down Expand Up @@ -83,10 +81,7 @@ func actionAdd(msg *tg.Message, pack bool) {
for _, sticker := range set.Stickers {
var exist bool
exist, err = dbAddSticker(
msg.From.ID,
sticker.SetName,
sticker.FileID,
sticker.Emoji,
msg.From.ID, sticker.SetName, sticker.FileID, sticker.Emoji,
)
errCheck(err)

Expand All @@ -96,7 +91,6 @@ func actionAdd(msg *tg.Message, pack bool) {
}

log.Ln("All exists?", allExists)

if allExists {
reply.Text = T("error_already_add_pack", map[string]interface{}{
"SetTitle": set.Title,
Expand Down
30 changes: 11 additions & 19 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"strings"

// log "github.com/kirillDanshin/dlog"
log "github.com/kirillDanshin/dlog"
tg "github.com/toby3d/telegram"
)

Expand All @@ -19,31 +19,23 @@ const (
)

func commands(msg *tg.Message) {
T, err := switchLocale(msg.From.LanguageCode)
errCheck(err)

switch {
case strings.ToLower(msg.Command()) == strings.ToLower(cmdStart):
log.Ln("command:", msg.Command())
switch strings.ToLower(msg.Command()) {
case strings.ToLower(cmdStart):
commandStart(msg)
case strings.ToLower(msg.Command()) == strings.ToLower(cmdHelp):
case strings.ToLower(cmdHelp):
commandHelp(msg)
case msg.Text == T("button_add_sticker"),
strings.ToLower(msg.Command()) == strings.ToLower(cmdAddSticker):
case strings.ToLower(cmdAddSticker):
commandAdd(msg, false)
case msg.Text == T("button_add_pack"),
strings.ToLower(msg.Command()) == strings.ToLower(cmdAddPack):
case strings.ToLower(cmdAddPack):
commandAdd(msg, true)
case msg.Text == T("button_del_sticker"),
strings.ToLower(msg.Command()) == strings.ToLower(cmdDeleteSticker):
case strings.ToLower(cmdDeleteSticker):
commandDelete(msg, false)
case msg.Text == T("button_del_pack"),
strings.ToLower(msg.Command()) == strings.ToLower(cmdDeletePack):
case strings.ToLower(cmdDeletePack):
commandDelete(msg, true)
case msg.Text == T("button_reset"),
strings.ToLower(msg.Command()) == strings.ToLower(cmdReset):
case strings.ToLower(cmdReset):
commandReset(msg)
case msg.Text == T("button_cancel"),
strings.ToLower(msg.Command()) == strings.ToLower(cmdCancel):
case strings.ToLower(cmdCancel):
commandCancel(msg)
}
}
15 changes: 6 additions & 9 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strconv"
"strings"

log "github.com/kirillDanshin/dlog" // Insert logs only in debug builds
"github.com/tidwall/buntdb" // Redis-like database
log "github.com/kirillDanshin/dlog"
"github.com/tidwall/buntdb"
)

const (
Expand Down Expand Up @@ -57,11 +57,7 @@ func dbGetUsers() ([]int, error) {
func dbChangeUserState(userID int, state string) error {
log.Ln("Trying to change", userID, "state to", state)
return db.Update(func(tx *buntdb.Tx) error {
_, _, err := tx.Set(
fmt.Sprint("user:", userID, ":state"), // key
state, // val
nil, // options
)
_, _, err := tx.Set(fmt.Sprint("user:", userID, ":state"), state, nil)
return err
})
}
Expand Down Expand Up @@ -119,7 +115,9 @@ func dbDeleteSticker(userID int, setName, fileID string) (bool, error) {
}

err := db.Update(func(tx *buntdb.Tx) error {
_, err := tx.Delete(fmt.Sprint("user:", userID, ":set:", setName, ":sticker:", fileID))
_, err := tx.Delete(
fmt.Sprint("user:", userID, ":set:", setName, ":sticker:", fileID),
)
return err
})

Expand Down Expand Up @@ -215,7 +213,6 @@ func dbGetUserStickers(userID, offset int, query string) ([]string, int, error)
}

total++

if count >= 51 {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions get_updates_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"fmt"

log "github.com/kirillDanshin/dlog" // Insert logs only in debug builds
tg "github.com/toby3d/telegram" // My Telegram bindings
log "github.com/kirillDanshin/dlog"
tg "github.com/toby3d/telegram"
)

// allowedUpdates is a value for parameter of updates configuration
Expand Down
8 changes: 1 addition & 7 deletions i18n/en.all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,8 @@ error_unknown:
other: |-
I do not know what to do with this sticker.
Please run /{{.AddStickerCommand}}, /{{.AddPackCommand}}, /{{.DeleteStickerCommand}} or /{{.DeletePackCommand}} command first.
meta_key_phrase:
key_phrase:
other: Yes, I am totally sure.
meta_reset_1:
other: Wait. Who are you?
meta_reset_2:
other: What are you doing here?
meta_reset_3:
other: What am I doing here?
reply_add_pack:
other: Send an existing stickers from any other packs to add the entire packs to
yourself.
Expand Down
8 changes: 0 additions & 8 deletions i18n/en/en.meta.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions i18n/en/en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
key_phrase:
other: Yes, I am totally sure.
8 changes: 1 addition & 7 deletions i18n/ru.all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,8 @@ error_unknown:
other: |-
Я понятия не имею что делать с этим стикером.
Пожалуйста, сначала примени /{{.AddStickerCommand}}, /{{.AddPackCommand}}, /{{.DeleteStickerCommand}} или /{{.DeletePackCommand}}.
meta_key_phrase:
key_phrase:
other: Да, я абсолютно уверен.
meta_reset_1:
other: Подожди. Кто ты?
meta_reset_2:
other: Что ты здесь делаешь?
meta_reset_3:
other: Что Я здесь делаю?
reply_add_pack:
other: Пришли стикеры из любых других наборов чтобы целиком добавить их наборы в
свой.
Expand Down
8 changes: 0 additions & 8 deletions i18n/ru/ru.meta.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions i18n/ru/ru.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
key_phrase:
other: Да, я абсолютно уверен.
6 changes: 3 additions & 3 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"path/filepath"
"strings"

log "github.com/kirillDanshin/dlog" // Insert logs only in debug builds
"github.com/nicksnyder/go-i18n/i18n" // Internationalization and localization
"github.com/olebedev/config" // Easy configuration file parsing
log "github.com/kirillDanshin/dlog"
"github.com/nicksnyder/go-i18n/i18n"
"github.com/olebedev/config"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
log "github.com/kirillDanshin/dlog" // Insert logs only in debug builds
tg "github.com/toby3d/telegram" // My Telegram bindings
log "github.com/kirillDanshin/dlog"
tg "github.com/toby3d/telegram"
)

// bot is general structure of the bot
Expand Down
43 changes: 21 additions & 22 deletions messages.go
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
package main

import tg "github.com/toby3d/telegram"
import (
"strings"

tg "github.com/toby3d/telegram"
)

// message function check Message update on commands, sended stickers or other
// user stuff
func messages(msg *tg.Message) {
state, err := dbGetUserState(msg.From.ID)
T, err := switchLocale(msg.From.LanguageCode)
errCheck(err)

switch state {
case stateAddSticker:
actionAdd(msg, false)
case stateAddPack:
actionAdd(msg, true)
case stateDeleteSticker:
actionDelete(msg, false)
case stateDeletePack:
actionDelete(msg, true)
case stateReset:
actionReset(msg)
case stateNone:
actionError(msg)
default:
err = dbChangeUserState(msg.From.ID, stateNone)
errCheck(err)

messages(msg)
switch {
case strings.EqualFold(msg.Text, T("button_add_sticker")):
commandAdd(msg, false)
case strings.EqualFold(msg.Text, T("button_add_pack")):
commandAdd(msg, true)
case strings.EqualFold(msg.Text, T("button_del_sticker")):
commandDelete(msg, false)
case strings.EqualFold(msg.Text, T("button_del_pack")):
commandDelete(msg, true)
case strings.EqualFold(msg.Text, T("button_reset")):
commandReset(msg)
case strings.EqualFold(msg.Text, T("button_cancel")):
commandCancel(msg)
case strings.EqualFold(msg.Text, T("meta_key_phrase")):
actions(msg)
}
}
38 changes: 8 additions & 30 deletions reset.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package main

import (
"fmt"
"time"
"strings"

tg "github.com/toby3d/telegram" // My Telegram bindings
tg "github.com/toby3d/telegram"
)

func commandReset(msg *tg.Message) {
Expand All @@ -24,7 +23,6 @@ func commandReset(msg *tg.Message) {
reply := tg.NewMessage(msg.Chat.ID, T("error_already_reset"))
reply.ParseMode = tg.ModeMarkdown
reply.ReplyMarkup = getMenuKeyboard(T)

_, err = bot.SendMessage(reply)
errCheck(err)
return
Expand All @@ -33,15 +31,12 @@ func commandReset(msg *tg.Message) {
err = dbChangeUserState(msg.From.ID, stateReset)
errCheck(err)

reply := tg.NewMessage(
msg.Chat.ID,
T("reply_reset", map[string]interface{}{
"KeyPhrase": T("meta_key_phrase"),
"CancelCommand": cmdCancel,
}))
reply := tg.NewMessage(msg.Chat.ID, T("reply_reset", map[string]interface{}{
"KeyPhrase": T("meta_key_phrase"),
"CancelCommand": cmdCancel,
}))
reply.ParseMode = tg.ModeMarkdown
reply.ReplyMarkup = getCancelButton(T)

_, err = bot.SendMessage(reply)
errCheck(err)
}
Expand All @@ -56,11 +51,10 @@ func actionReset(msg *tg.Message) {
_, err = bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
errCheck(err)

if msg.Text != T("meta_key_phrase") {
if !strings.EqualFold(msg.Text, T("meta_key_phrase")) {
reply := tg.NewMessage(msg.Chat.ID, T("error_reset_phrase"))
reply.ParseMode = tg.ModeMarkdown
reply.ReplyMarkup = getMenuKeyboard(T)

_, err = bot.SendMessage(reply)
errCheck(err)
return
Expand All @@ -71,23 +65,7 @@ func actionReset(msg *tg.Message) {

reply := tg.NewMessage(msg.Chat.ID, T("success_reset"))
reply.ParseMode = tg.ModeMarkdown
reply.ReplyMarkup = tg.NewReplyKeyboardRemove(false)

reply.ReplyMarkup = getMenuKeyboard(T)
_, err = bot.SendMessage(reply)
errCheck(err)

for i := 1; i <= 3; i++ {
_, err = bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
errCheck(err)

text := T(fmt.Sprint("meta_reset_", i))

time.Sleep(time.Minute * time.Duration(len(text)) / 1000)

reply = tg.NewMessage(msg.Chat.ID, text)
reply.ParseMode = tg.ModeMarkdown

_, err = bot.SendMessage(reply)
errCheck(err)
}
}
Loading

0 comments on commit 7443022

Please sign in to comment.