-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (93 loc) · 2.95 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
package main
import (
"fmt"
"os"
"github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/sadlil/gologger"
"io/ioutil"
"strings"
"regexp"
)
var (
logger gologger.GoLogger
)
func main() {
s, err := ioutil.ReadFile("buglist")
if err != nil {
fmt.Errorf("cannot read source material, make sure you placed it in 'buglist' file\n %s", err)
}
cs := string(s)
regexpC := regexp.MustCompile(`(?:\[.+] |\[.+]: |\[.+]|\[.+]:)(.+)`)
regexpF := regexp.MustCompile(`(?:{.+} |{.+}|{.+] |{.+])(.+)`)
chain := NewChain()
sl := strings.Split(cs, "\n")
for _, line := range sl {
if line[0] == '[' {
r := regexpC.FindStringSubmatch(line)
line = r[1]
}
if line[0] == '{' {
r := regexpF.FindStringSubmatch(line)
line = r[1]
}
if len(line) > 3 {
if line[len(line)-1] == ' ' && line[len(line)-2] == ' ' {
line = line[:len(line)-2]
} else if line[len(line)-1] == ' ' {
line = line[:len(line)-1]
}
chain.AddSentence(strings.Split(line, " "))
}
}
logger = gologger.GetLogger(gologger.CONSOLE, gologger.ColoredLog)
token := os.Getenv("TGBOTTOKEN")
if token == "" || token == "token" {
panic("you must set TGBOTTOKEN before starting")
}
bot, err := tgbotapi.NewBotAPI(token)
if err != nil {
panic(err)
}
bot.Debug = false
production := os.Getenv("TGPROD")
if production != "1" {
bot.Debug = true
}
logger.Info(fmt.Sprintf("Authorized as @%s", bot.Self.UserName))
u := tgbotapi.NewUpdate(0)
u.Timeout = 80
updates, err := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil {
continue
}
if !update.Message.IsCommand() {
continue
}
logger.Info(fmt.Sprintf("%d (@%s) in %d (%q) executed %q with arguments %q", update.Message.From.ID, update.Message.From.UserName, update.Message.Chat.ID, update.Message.Chat.Title, update.Message.Command(), update.Message.CommandArguments()))
switch update.Message.Command() {
case "bug":
msg := tgbotapi.NewMessage(update.Message.Chat.ID, chain.RandomSentence())
if update.Message.Chat.Type != "private" {
msg.ReplyToMessageID = update.Message.MessageID
}
bot.Send(msg)
case "help":
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "*Markov chain based bug generator.*\n\nWritten by [raccoon](t.me/Rirush), inspired by [melulza](t.me/Melulza), powered by [gophers](golang.org).\n\nType /bug to create a bug.")
msg.ParseMode = "markdown"
msg.DisableWebPagePreview = true
if update.Message.Chat.Type != "private" {
msg.ReplyToMessageID = update.Message.MessageID
}
bot.Send(msg)
case "start":
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "*Markov chain based bug generator.*\n\nWritten by [raccoon](t.me/Rirush), inspired by [melulza](t.me/Melulza), powered by [gophers](golang.org).\n\nType /bug to create a bug.")
msg.ParseMode = "markdown"
msg.DisableWebPagePreview = true
if update.Message.Chat.Type != "private" {
msg.ReplyToMessageID = update.Message.MessageID
}
bot.Send(msg)
}
}
}