-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathxyebot.go
67 lines (60 loc) · 1.72 KB
/
xyebot.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
package main
import (
"encoding/json"
"log"
"net/http"
"os"
)
const (
// BotName is the telegram name of the bot. Needs for commands identification
BotName = "xye_bot"
// DelayLimit is the upper limit of randomly skipped messages.
// for example, 4 means bot would skip random amount of messages between 1 and 4.
DelayLimit = 4
// WordsAmount is the amount of words from the message to apply modification
WordsAmount = 1
)
var delayMap map[int64]int
func sendMessage(w http.ResponseWriter, chatID int64, text string, replyToID *int64, parseMode string) {
var msg Response
if replyToID == nil {
msg = Response{ChatID: chatID, Text: text, Method: "sendMessage", ParseMode: parseMode, DisableWebPagePreview: true}
} else {
msg = Response{ChatID: chatID, Text: text, ReplyToID: replyToID, Method: "sendMessage", ParseMode: parseMode, DisableWebPagePreview: true}
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(msg)
}
func handler(w http.ResponseWriter, r *http.Request) {
request, err := newRequest(w, r)
if err != nil {
return
}
if err = request.handleCommand(); err == nil {
return
}
request.handleDelay()
if !request.isAnswerNeeded() {
return
}
output := request.Modify()
if output == "" {
return
}
request.cleanDelay()
sendMessage(request.writer, request.updateMessage.Chat.ID, output, &request.updateMessage.ID, "")
}
func main() {
delayMap = make(map[int64]int)
http.HandleFunc("/", handler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}