mirror of
https://gitlab.com/prokop_paruzek/botik.git
synced 2025-12-19 06:17:56 +01:00
add Chuck Norris jokes
This commit is contained in:
37
irc_bot.go
37
irc_bot.go
@@ -3,9 +3,12 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -28,6 +31,16 @@ Příkazy:
|
|||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ChuckJoke struct {
|
||||||
|
Categories []interface{} `json:"categories"`
|
||||||
|
Created_at string `json:"created_at"`
|
||||||
|
Icon_url string `json:"icon_url"`
|
||||||
|
Id string `json:"id"`
|
||||||
|
Update_at string `json:"update_at"`
|
||||||
|
Url string `json:"url"`
|
||||||
|
Value string `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
func sendMsg(conn io.Writer, msg string, receiver string) {
|
func sendMsg(conn io.Writer, msg string, receiver string) {
|
||||||
fmt.Fprintln(conn, "PRIVMSG "+receiver+" :"+msg)
|
fmt.Fprintln(conn, "PRIVMSG "+receiver+" :"+msg)
|
||||||
}
|
}
|
||||||
@@ -88,9 +101,27 @@ func main() {
|
|||||||
fmt.Println(string(msg))
|
fmt.Println(string(msg))
|
||||||
sendMultiline(conn, string(msg), receiver)
|
sendMultiline(conn, string(msg), receiver)
|
||||||
case "!ftip":
|
case "!ftip":
|
||||||
ftip, _ := api.Fetch()
|
if rand.Intn(2) == 0 {
|
||||||
for _, l := range ftip.Joke {
|
res, err := http.Get("https://api.chucknorris.io/jokes/random")
|
||||||
sendMultiline(conn, l, receiver)
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
var parse ChuckJoke
|
||||||
|
bodyStr, err := io.ReadAll(res.Body)
|
||||||
|
res.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(bodyStr, &parse)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
sendMultiline(conn, parse.Value, receiver)
|
||||||
|
} else {
|
||||||
|
ftip, _ := api.Fetch()
|
||||||
|
for _, l := range ftip.Joke {
|
||||||
|
sendMultiline(conn, l, receiver)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user