/
chuckbot.go
93 lines (79 loc) · 1.82 KB
/
chuckbot.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
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"html"
"io/ioutil"
"net/http"
"strings"
"github.com/thoj/go-ircevent"
)
var j = make(chan string)
var con *irc.Connection
var server string
var channel string
var nick string
func init() {
flag.StringVar(&nick, "nick", "chuckbot", "the nick for the bot")
flag.StringVar(&channel, "channel", "", "the channel to join")
flag.StringVar(&server, "server", "", "the <server-ip>:<port> to connect to")
flag.Parse()
if server == "" {
panic("no server defined. use flag '-server <host>:<port>' to set it.")
}
if channel == "" {
panic("no channel defined. use flag '-channel <channelname>' to set it.")
}
}
func main() {
con = irc.IRC(nick, nick)
con.Debug = true
con.UseTLS = true
con.TLSConfig = &tls.Config{InsecureSkipVerify: true}
fmt.Printf("%v\n", con)
err := con.Connect(server)
if err != nil {
panic(err)
}
defer con.Quit()
con.AddCallback("001", func(e *irc.Event) { con.Join(channel) })
con.AddCallback("PRIVMSG", func(e *irc.Event) {
// only talk when prompted
if strings.Contains(strings.ToLower(e.Message()), strings.ToLower(nick)) {
con.Privmsg(e.Arguments[0], fmt.Sprintf("Apropos, %s: %s", e.Nick, <-j))
}
})
go getQuote()
con.Loop()
}
type J struct {
Id uint
Joke string
Categories []string
}
type Q struct {
Type string
Value J
}
func getQuote() {
for {
resp, err := http.Get("http://api.icndb.com/jokes/random?firstName=Chuckbot&lastName=Norris")
if err != nil {
fmt.Printf("ERROR: %v\n", err)
}
quote := Q{}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
}
fmt.Printf("%v\n", body)
err = json.Unmarshal(body, "e)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
}
resp.Body.Close()
j <- html.UnescapeString(quote.Value.Joke)
}
}