-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokemove.go
166 lines (141 loc) · 4.14 KB
/
pokemove.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"io/ioutil"
"strings"
"strconv"
"os"
"github.com/urfave/negroni"
"github.com/juz501/go_logger_middleware"
)
func main() {
logfilename := "log/pokemove.log"
errorLog, err := os.OpenFile( logfilename , os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666 )
if err != nil {
log.Fatal( "error writing to log: " + logfilename )
}
defer errorLog.Close()
n := negroni.New()
l := go_logger_middleware.NewLoggerWithStream( errorLog )
m := http.NewServeMux()
handleRoutes( m, l )
n.Use( l )
n.UseHandler( m )
log.Fatal( http.ListenAndServe( ":18885", n ) )
}
func handleRoutes( m *http.ServeMux, l *go_logger_middleware.Logger ) {
m.HandleFunc( "/", func( w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
text := r.URL.Query().Get( "text" )
url := ""
response_url := r.URL.Query().Get( "response_url" )
if response_url != "" {
url = response_url
} else {
w.WriteHeader( http.StatusOK )
return
}
if text == "" {
fmt.Fprintf( w, "%s", about() )
} else {
go sendMoveInfo( url, text, w, l )
fmt.Fprintf( w, "loading move data" )
}
default:
w.WriteHeader( http.StatusOK )
}
})
m.HandleFunc( "/favicon.ico", func( w http.ResponseWriter, r *http.Request) {
fmt.Fprintf( w, "%s", about() )
})
}
func about() ( s string ) {
s = fmt.Sprint( "Just type your move after the /pokemove command" )
return
}
type Message struct {
Text string `json:"text"`
Markdown bool `json:"mrkdwn"`
ReplaceOriginal bool `json:"replace_original"`
}
func sendMoveInfo( url string, move_name string, w http.ResponseWriter, l *go_logger_middleware.Logger ) {
s := getMoveResult( move_name )
l.Println( move_name )
m := Message{ s, true, true }
message, err := json.Marshal( m )
if err != nil {
panic( "json marshall failed" )
}
_ = sendToSlack( url, message )
}
func sendToSlack( url string, message []byte) (resp *http.Response) {
client := http.Client{}
body := bytes.NewBuffer( message )
req, err := http.NewRequest( "POST", url, body )
if err != nil {
return
}
req.Header.Set( "Content-Type", "application/json" )
resp, _ = client.Do( req )
defer resp.Body.Close()
return
}
type Data struct {
Accuracy int64 `json:"accuracy"`
EffectEntries []Effect `json:"effect_entries"`
EffectChance int64 `json:"effect_chance"`
Name string `json:"name"`
PP int64 `json:"pp"`
Power int64 `json:"power"`
Target Target `json:"target"`
Type Type `json:"type"`
}
type Effect struct {
Language Language `json:"language"`
Effect string `json:"effect"`
ShortEffect string `json:"short_effect"`
}
type Language struct {
Name string `json:"name"`
}
type Target struct {
Name string `json:"name"`
}
type Type struct {
Name string `json:"name"`
}
func getMoveResult( move_name string ) ( move_desc string ) {
res, err := http.Get( "http://pokeapi.co/api/v2/move/" + move_name )
if err != nil {
move_desc = ""
return
}
move_data, err := ioutil.ReadAll( res.Body )
var m Data
_ = json.Unmarshal( move_data, &m )
if err != nil {
move_desc = ""
return
}
if m.Name != "" {
move_desc = "*" + strings.Replace( strings.ToUpper( m.Name ), "-", " ", -1 ) + ":* \n"
move_desc += "• Accuracy : " + strconv.FormatInt( m.Accuracy, 10 ) + "\n"
move_desc += "• PP : " + strconv.FormatInt( m.PP, 10 ) + "\n"
move_desc += "• Power : " + strconv.FormatInt( m.Power, 10 ) + "\n"
move_desc += "• Target : " + m.Target.Name + "\n"
move_desc += "• Type : " + m.Type.Name + "\n"
effect_chance := strconv.FormatInt( m.EffectChance, 10 )
move_desc += "• Effect Chance : " + effect_chance + "\n"
for _, entry := range m.EffectEntries {
move_desc += "• Language : " + entry.Language.Name + "\n"
move_desc += "• Effect short : " + strings.Replace( entry.ShortEffect, "$effect_chance", effect_chance, -1 ) + "\n"
move_desc += "• Effect : \n>>>" + strings.Replace( entry.Effect, "$effect_chance", effect_chance, -1 ) + "\n"
}
}
return
}