-
Notifications
You must be signed in to change notification settings - Fork 4
/
bot.go
162 lines (127 loc) · 3.34 KB
/
bot.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"encoding/json"
"fmt"
"github.com/garyburd/redigo/redis"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
func sendCommand(method, token string, params url.Values) ([]byte, error) {
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s?%s",
token, method, params.Encode())
timeout := 35 * time.Second
client := http.Client{
Timeout: timeout,
}
resp, err := client.Get(url)
if err != nil {
return []byte{}, err
}
resp.Close = true
defer resp.Body.Close()
json, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
return json, nil
}
func (bot *Bot) Commands(command string, chat int) {
markov := Markov{20}
word := strings.Split(command, " ")
seed := strings.Join(word[1:], " ") // Removes the initial command
if word[0] == "/chobot" && len(word) >= 2 {
text := markov.Generate(seed, bot.Connection)
bot.Say(text, chat)
} else if word[0] == "/chosource" {
text := fmt.Sprintf("Author: %v \nSource: %v",
"@blackdev1l",
"https://github.com/blackdev1l/ritalobot")
bot.Say(text, chat)
}
}
type Bot struct {
Token string
Connection redis.Conn
Chance int
}
func (bot Bot) GetUpdates() []Result {
offset, _ := redis.String(bot.Connection.Do("GET", "update_id"))
params := url.Values{}
params.Set("offset", offset)
params.Set("timeout", strconv.Itoa(30))
resp, err := sendCommand("getUpdates", token, params)
if err != nil {
log.Println(err)
}
var updatesRecieved Response
json.Unmarshal(resp, &updatesRecieved)
if !updatesRecieved.Ok {
err = fmt.Errorf("chobot: %s\n", updatesRecieved.Description)
return nil
}
var updates = updatesRecieved.Result
if len(updates) != 0 {
updateID := updates[len(updates)-1].Update_id + 1
bot.Connection.Do("SET", "update_id", updateID)
return updates
}
return nil
}
func (bot Bot) Say(text string, chat int) (bool, error) {
var responseRecieved struct {
Ok bool
Description string
}
params := url.Values{}
params.Set("chat_id", strconv.Itoa(chat))
params.Set("text", text)
resp, err := sendCommand("sendMessage", token, params)
err = json.Unmarshal(resp, &responseRecieved)
if err != nil {
return false, err
}
if !responseRecieved.Ok {
return false, fmt.Errorf("chobot: %s\n", responseRecieved.Description)
}
return responseRecieved.Ok, nil
}
func (bot Bot) Listen() {
var err error
rand.Seed(time.Now().UnixNano())
bot.Chance = chance
tmp := ":" + strconv.Itoa(port)
bot.Connection, err = redis.Dial(connection, tmp)
if err != nil {
fmt.Println("connection to redis failed")
log.Fatal(err)
}
fmt.Printf("redis connection: %v | port is %v\n", connection, port)
fmt.Printf("chance rate %v%!\n", bot.Chance)
bot.Poll()
}
func (bot Bot) Poll() {
markov := Markov{10}
for {
updates := bot.GetUpdates()
if updates != nil {
markov.StoreUpdates(updates, bot.Connection)
if strings.HasPrefix(updates[0].Message.Text, "/cho") {
bot.Commands(updates[0].Message.Text,
updates[0].Message.Chat.Id)
} else if rand.Intn(100) <= bot.Chance {
in_text := updates[len(updates)-1].Message.Text
parts := strings.Split(in_text, " ")
seed := parts[0] // Seed the chain with the first word only
chat := updates[len(updates)-1].Message.Chat.Id
out_text := markov.Generate(seed, bot.Connection)
bot.Say(out_text, chat)
}
}
}
}