Skip to content

Commit

Permalink
version 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezvara committed Oct 20, 2019
1 parent 6a2822d commit 8ef52dd
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 169 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ USER COMMANDS
/settext <id> <text> - updates the expected text to be found on every monitor request
/settimeout <id> <timeout> - updates the expected timeout to be found on every monitor request
/setstatuscode <id> <statuscode> - updates the expected HTTP status code for the monitor
/subscribe <id> - subscrives to monitor status
/subscribe <id> - subscribes to monitor status
/unsubscribe <id> - unsubscribes from monitor status
/test <method> <url> <statuscode> - test a URL and its HTTP status
/testfull <method> <url> <statuscode> - test a URL and its HTTP status, returning its data
Expand Down Expand Up @@ -127,9 +127,14 @@ GLOBAL OPTIONS:
# Changes:
0.3.1:
- Payload refactor, payload operations are done on the handler instead of all commands code
- Added '/command help' with a longer explanation for each command.
0.3.0:
- Problems now explain the failure
- Problems now explain the failure that triggered the alert
- Markdown messages updated
0.2.1:
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mkdir -p bin
rm -rf bin/*

VERSION="0.3.0"
VERSION="0.3.1"
BUILD_DATE=$(date +%Y-%m-%d_%H:%M:%S)
COMMIT=$(git rev-parse HEAD)
gox -ldflags "-X main.Version=$VERSION -X main.Commit=$COMMIT -X main.BuildDate=$BUILD_DATE" -rebuild -output "./bin/{{.Dir}}_{{.OS}}_{{.Arch}}"
Expand Down
36 changes: 15 additions & 21 deletions cmd_admins.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
tb "gopkg.in/tucnak/telebot.v2"
)

// grantOrRevoke modifies the authorized status for a user
func (u *urlTester) users(m *tb.Message) {
// users returns current users of the bot and statuses
func (u *urlTester) users(m *tb.Message, returns []interface{}) {

u.saveHistory(m)

Expand All @@ -18,7 +18,7 @@ func (u *urlTester) users(m *tb.Message) {

err = u.db.All(&users)
if err != nil {
u.bot.Send(m.Sender, fmt.Sprintf("There was an error: %s", err.Error()))
u.explainError(m, "", err)
return
}

Expand All @@ -29,7 +29,7 @@ func (u *urlTester) users(m *tb.Message) {
message = fmt.Sprintf("%s @%s", message, thisUser.Username)
}
if thisUser.Authorized {
message = fmt.Sprintf("%s *(autorized)*", message)
message = fmt.Sprintf("%s *(authorized)*", message)
}
if u.isUserAdmin(thisUser.ID) {
message = fmt.Sprintf("%s _(admin)_", message)
Expand All @@ -41,48 +41,42 @@ func (u *urlTester) users(m *tb.Message) {

}

func (u *urlTester) grant(m *tb.Message) {
func (u *urlTester) grant(m *tb.Message, returns []interface{}) {

u.grantOrRevoke(m, true)
u.grantOrRevoke(m, true, returns)

}

func (u *urlTester) revoke(m *tb.Message) {
func (u *urlTester) revoke(m *tb.Message, returns []interface{}) {

u.grantOrRevoke(m, false)
u.grantOrRevoke(m, false, returns)

}

// grantOrRevoke modifies the authorized status for a user
func (u *urlTester) grantOrRevoke(m *tb.Message, action bool) {
func (u *urlTester) grantOrRevoke(m *tb.Message, action bool, returns []interface{}) {

u.saveHistory(m)

var (
id int
returns []interface{}
err error
tgUser user
)
id int

returns, err = u.payloadReader(m.Text)
if err != nil {
u.bot.Send(m.Sender, err.Error())
return
}
err error
tgUser user
)

id = returns[0].(int)

err = u.db.One("ID", id, &tgUser)
if err != nil {
u.bot.Send(m.Sender, fmt.Sprintf("There was an error: %s", err.Error()))
u.explainError(m, "", err)
return
}

tgUser.Authorized = action
err = u.db.Save(&tgUser)
if err != nil {
u.bot.Send(m.Sender, fmt.Sprintf("There was an error: %s", err.Error()))
u.explainError(m, "", err)
return
}

Expand Down
35 changes: 28 additions & 7 deletions cmd_handler.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
package main

import (
"fmt"
"log"
"strings"

tb "gopkg.in/tucnak/telebot.v2"
)

func (u *urlTester) handler(m *tb.Message) {

var (
cmdString string
returns []interface{}
err error
)

if m.Text == "" {
return
}

log.Printf("Command received: UserID: %d. Text: '%s'", m.Sender.ID, m.Text)
parts := strings.Split(m.Text, " ")
log.Printf("Command received: User: %s %s (%d). Text: '%s'", m.Sender.FirstName, m.Sender.LastName, m.Sender.ID, m.Text)

cmd, ok := u.commands[parts[0]]
if !ok {
u.bot.Send(m.Sender, "Command not found. Use /help.")
cmdString, returns, err = u.payloadReader(m.Text)
if err != nil {
u.explainError(m, cmdString, err)
return
}

cmd := u.commands[cmdString]

// check if command is private
if cmd.isPrivate == true {
if !m.Private() {
Expand All @@ -43,6 +50,20 @@ func (u *urlTester) handler(m *tb.Message) {
}
}

cmd.fn(m)
cmd.fn(m, returns)

}

func (u *urlTester) explainError(m *tb.Message, cmdString string, err error) {

switch err {
case errInvalidPayload:
// get the command and it to the help func
u.help(m, []interface{}{cmdString})
case errCommandNotFound:
u.bot.Send(m.Sender, fmt.Sprintf("'%s' command not found.\n Use /help for more info.", cmdString))
default:
u.explainError(m, "", err)
}

}
48 changes: 41 additions & 7 deletions cmd_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,52 @@ func (u *urlTester) argsToString(payload []payloadPart) (message string) {

}

func (u *urlTester) help(m *tb.Message) {
func (u *urlTester) help(m *tb.Message, returns []interface{}) {

var (
anonMessage string
usersMessage string
adminsMessage string
message string
cmdString string
cmd command
)

// build all commands message
if m.Payload == "" {
switch len(returns) {
case 1:

cmdString = returns[0].(string)
cmd = u.commands[cmdString]

// one command help
message = fmt.Sprintf("%s %s\n\n%s", cmdString, u.argsToString(cmd.payload), cmd.helpLong)

if len(cmd.payload) > 0 {
message = fmt.Sprintf("%s\n\n*Options*:\n", message)
}
for _, payloadPart := range cmd.payload {
message = fmt.Sprintf("%s*<%s:%s>* - %s\n", message, payloadPart.arg, payloadPart.typ, payloadPart.help)
if len(payloadPart.valid) > 0 {
message = fmt.Sprintf("%sAllowed:", message)
for i, p := range payloadPart.valid {
if i == 0 {
message = fmt.Sprintf("%s %s", message, p)
} else {
message = fmt.Sprintf("%s,%s", message, p)
}
}
message = fmt.Sprintf("%s\n", message)
}
}

if _, err := u.bot.Send(m.Sender, message, tb.ModeMarkdown); err != nil {
log.Println(err)
}
return

default:

// build all commands message
for key, value := range u.commands {
if !value.noHelp {
if value.forUsers == false && value.forAdmins == false {
Expand Down Expand Up @@ -57,19 +91,19 @@ func (u *urlTester) help(m *tb.Message) {

// admins commands
if u.isUserAdmin(m.Sender.ID) {
message = fmt.Sprintf(`%s
*ADMIN COMMANDS*
message = fmt.Sprintf(`%s*ADMIN COMMANDS*
%s
`, message, adminsMessage)
}

message = fmt.Sprintf("%s\n\nFor a longer explanation of a command, use:\n/command help\n", message)

if _, err := u.bot.Send(m.Sender, message, tb.ModeMarkdown); err != nil {
log.Println(err)
}
return
}

}
// one command help

}
5 changes: 2 additions & 3 deletions cmd_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
tb "gopkg.in/tucnak/telebot.v2"
)

func (u *urlTester) history(m *tb.Message) {
func (u *urlTester) history(m *tb.Message, returns []interface{}) {

var (
err error
Expand All @@ -20,8 +20,7 @@ func (u *urlTester) history(m *tb.Message) {
message = "-- History --\n"
err = u.db.Find("UserID", m.Sender.ID, &histories, storm.Limit(20), storm.Reverse()) // the last 20 messages or less
if err != nil {
u.bot.Send(m.Sender, "there was an error retrieving information")
u.bot.Send(m.Sender, err.Error())
u.explainError(m, "", err)
return
}

Expand Down
Loading

0 comments on commit 8ef52dd

Please sign in to comment.