/
external.go
47 lines (37 loc) · 999 Bytes
/
external.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package command
import (
"bytes"
"os/exec"
"regexp"
"strconv"
log "github.com/sirupsen/logrus"
"github.com/ssalvatori/zbot-telegram-go/user"
)
//ExternalCommand definition
type ExternalCommand struct {
PathModules string
}
// ProcessText run command
func (handler *ExternalCommand) ProcessText(text string, user user.User) (string, error) {
commandPattern := regexp.MustCompile(`^!([a-zA-Z0-9\_\-]+)([\s(\S*)]*)?`)
if commandPattern.MatchString(text) {
args := commandPattern.FindStringSubmatch(text)
externalModule := args[1]
log.Debug("Looking for module: " + handler.PathModules + externalModule)
binary, err := exec.LookPath(handler.PathModules + externalModule)
if err != nil {
log.Error(err)
return "", err
}
cmd := exec.Command(binary, user.Username, strconv.Itoa(user.Level), args[2])
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
log.Error(err)
return "", err
}
return out.String(), nil
}
return "", nil
}