/
ritalobot.go
79 lines (61 loc) · 1.46 KB
/
ritalobot.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"gopkg.in/yaml.v2"
)
var (
port int
token string
connection string
configPath string
chance int
)
type Config struct {
Token string `yaml:"token"`
Chance int `yaml: "chance"`
Connection string `yaml:"connection"`
Port int `yaml:"port"`
}
func printLogo() {
file, _ := ioutil.ReadFile("logo")
fmt.Println(string(file))
}
func readConfig(configPath string) int {
filename, _ := filepath.Abs(configPath)
file, err := ioutil.ReadFile(filename)
if err != nil {
log.Println("no config file found")
log.Println(err)
return 1
}
var c Config
err = yaml.Unmarshal(file, &c)
if err != nil {
log.Println("error while parsing config.yaml, closing")
log.Fatalln(err)
}
token = c.Token
chance = c.Chance
connection = c.Connection
port = c.Port
return 0
}
func main() {
printLogo()
flag.StringVar(&token, "token", "", "authentication token for the telegram bot")
flag.StringVar(&connection, "conn", "tcp", "type of connection and/or ip of redis database")
flag.IntVar(&port, "p", 6379, "port number of redis database")
flag.StringVar(&configPath, "c", "./config.yml", "path for ritalobot config")
flag.IntVar(&chance, "chance", 10, "chance to say something after a message")
flag.Parse()
readConfig(configPath)
if token == "" {
log.Fatalln("authentication token not valid, use config or flags to pass it")
}
bot := Bot{}
bot.Listen()
}