-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
139 lines (112 loc) · 3.13 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/tecnoscimmie/tsredditbot/reddit"
"github.com/tecnoscimmie/tsredditbot/support"
)
var baseURL = ""
var conf support.ConfigFile
var err error
var redditUsername string
var redditPassword string
var redditClientID string
var redditClientSecret string
var debug bool
var botSession support.Session
func main() {
// parse ALL the parameters!
parametersParser()
botSession, err = support.NewSession()
if err != nil {
log.Fatal(err)
}
// start the bot!
fmt.Println("--> Starting tsreddit bot")
err = botSession.PrintBotInformations()
if err != nil {
log.Fatal(err)
}
http.HandleFunc("/"+botSession.Endpoint, endpointHandler)
log.Fatal("main http server error -> ", http.ListenAndServeTLS(":"+botSession.Port, botSession.Configuration.CertPath, botSession.Configuration.KeyPath, nil))
}
// handle the webhook data
func endpointHandler(w http.ResponseWriter, r *http.Request) {
var data support.TelegramObject
defer r.Body.Close()
err := data.DecodeJSON(r.Body)
if err != nil {
log.Println(err)
return
}
if debug {
log.Printf("got message -> %+v\n", data)
}
if data.Message.Chat.ID == botSession.ChatID {
log.Println("got URL from command /post on group ", botSession.GroupHandle, "posting on reddit...")
url, err := splitPostCommand(data.Message.Text)
if debug {
log.Println("splitted url:", url)
}
if err != nil {
log.Println(err)
return
}
validURL, err := support.ValidateURL(url)
if err != nil {
if err = data.ReplyBackToChat("Invalid URL :("); err != nil {
log.Println(err)
return
}
log.Println(err)
return
}
if debug {
log.Println("validated url:", validURL)
}
redditSession, err := reddit.NewSession(redditUsername, redditPassword, redditClientID, redditClientSecret)
if err != nil {
log.Println(err)
return
}
if err = redditSession.Post(validURL); err != nil {
log.Println(err)
return
}
if err = data.ReplyBackToChat("Posted!"); err != nil {
log.Println(err)
return
}
}
}
func parametersParser() {
// redefine flag.Usage(), because a little bit of branding is always good
flag.Usage = func() {
fmt.Printf("tsreddit: TecnoScimmie's reddit posting bot.\n\nUsage:\n")
flag.PrintDefaults()
}
// define and parse all the parameters available
flag.StringVar(&redditUsername, "username", "", "reddit bot username")
flag.StringVar(&redditPassword, "password", "", "reddit bot password")
flag.StringVar(&redditClientID, "clientid", "", "reddit bot client ID")
flag.StringVar(&redditClientSecret, "secret", "", "reddit bot secret")
flag.BoolVar(&debug, "debug", false, "enable debug output")
flag.Parse()
// do we have all the parameters needed to run?
if redditClientID == "" || redditClientSecret == "" || redditUsername == "" || redditPassword == "" {
flag.Usage()
os.Exit(1)
}
}
func splitPostCommand(c string) (string, error) {
// split the string for 6 characters (the "/post " command, space included)
if strings.HasPrefix(c, "/post") {
return strings.TrimSpace(c[6:]), nil
}
return "", errors.New("string is not a \"/post\" command")
}