forked from jakevoytko/crbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremotehastebin.go
58 lines (47 loc) · 1.71 KB
/
remotehastebin.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
package api
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strconv"
"github.com/jakevoytko/crbot/log"
)
// RemoteHastebin implements Gist, and interacts with the Hastebin API.
type RemoteHastebin struct{}
// NewRemoteHastebin works as advertised.
func NewRemoteHastebin() *RemoteHastebin {
return &RemoteHastebin{}
}
const (
msgHastebinPostFail = "Unable to connect to Hastebin service. Give it a few minutes and try again"
msgHastebinResponseFail = "Failure reading response from Hastebin service"
msgHastebinStatusCode = "Failed to upload Hastebin :("
msgHastebinURLFail = "Failed getting url from Hastebin service"
)
// Upload uploads the given string to hastebin and returns the URL of the hastebin on success.
func (g *RemoteHastebin) Upload(contents string) (string, error) {
response, err := http.Post(
"https://hastebin.com/documents", "application/x-www-form-urlencoded", bytes.NewBufferString(contents))
if err != nil {
log.Info("Error POSTing Hastebin", err)
return "", errors.New(msgHastebinPostFail)
}
defer response.Body.Close()
if response.StatusCode != 200 {
log.Info("Bad status code", errors.New("Code: "+strconv.Itoa(response.StatusCode)))
body, _ := ioutil.ReadAll(response.Body)
log.Info("Response body: ", errors.New(string(body)))
return "", errors.New(msgHastebinStatusCode)
}
responseMap := map[string]interface{}{}
if err := json.NewDecoder(response.Body).Decode(&responseMap); err != nil {
log.Info("Error reading Hastebin response", err)
return "", errors.New(msgHastebinResponseFail)
}
if finalURL, ok := responseMap["key"]; ok {
return "https://hastebin.com/raw/" + finalURL.(string), nil
}
return "", errors.New(msgHastebinURLFail)
}