/
py.go
50 lines (42 loc) · 1020 Bytes
/
py.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
48
49
50
package plugin
/*
usage: !py "hello%20world".title()
*/
import (
"fmt"
"github.com/dysfn/gerri/cmd"
"github.com/dysfn/gerri/data"
"io/ioutil"
"net/http"
"strings"
)
func PyResult(config *data.Config, statement string) (string, error) {
var result string
resource := fmt.Sprintf("%s/%s", config.PyApi, statement)
resp, err := http.Get(resource)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
result = strings.Replace(string(body), "\n", " ", -1)
result = strings.TrimSpace(result)
if err != nil {
return result, err
}
return result, nil
}
func ReplyPy(pm data.Privmsg, config *data.Config) (string, error) {
if len(pm.Message) >= 2 {
statement := strings.TrimSpace(strings.Join(pm.Message[1:], " "))
result, err := PyResult(config, statement)
if err != nil {
return "", err
}
if result != "" {
return cmd.Privmsg(pm.Target, result), nil
}
return cmd.PrivmsgAction(pm.Target, "zzzzz..."), nil
}
return "zzzzz...", nil
}