redis client, first step of message parsing
This commit is contained in:
parent
6760923bbb
commit
e8f52ce638
7 changed files with 183 additions and 5 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
|||
gempbotgo
|
||||
vendor/
|
||||
glide.lock
|
||||
.idea/
|
||||
.idea/
|
||||
config.json
|
9
config.example.json
Normal file
9
config.example.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"irc_address": "irc.chat.twitch.tv:6667",
|
||||
"irc_user": "gempbot",
|
||||
"irc_token": "oauth:123123123",
|
||||
"api_port": "8025",
|
||||
"redis_address": "127.0.0.1:6379",
|
||||
"redis_password": "asdasd",
|
||||
"redis_database": 0
|
||||
}
|
|
@ -2,3 +2,5 @@ package: github.com/gempir/gempbotgo
|
|||
import:
|
||||
- package: github.com/op/go-logging
|
||||
version: ^1.0.0
|
||||
- package: gopkg.in/redis.v5
|
||||
version: ^5.2.9
|
||||
|
|
22
main.go
22
main.go
|
@ -6,6 +6,8 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/op/go-logging"
|
||||
"github.com/gempir/gempbotgo/twitch"
|
||||
"gopkg.in/redis.v3"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -15,9 +17,13 @@ var (
|
|||
)
|
||||
|
||||
type config struct {
|
||||
IrcAddress string `json:"irc_address"`
|
||||
BrokerPass string `json:"broker_pass"`
|
||||
APIPort string `json:"api_port"`
|
||||
IrcAddress string `json:"irc_address"`
|
||||
IrcUser string `json:"irc_user"`
|
||||
IrcToken string `json:"irc_token"`
|
||||
APIPort string `json:"api_port"`
|
||||
RedisAddress string `json:"redis_address"`
|
||||
RedisPassword string `json:"redis_password"`
|
||||
RedisDatabase int64 `json:"redis_database"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -28,6 +34,14 @@ func main() {
|
|||
Log.Fatal(err)
|
||||
}
|
||||
|
||||
rClient := redis.NewClient(&redis.Options{
|
||||
Addr: cfg.RedisAddress,
|
||||
Password: cfg.RedisPassword,
|
||||
DB: cfg.RedisDatabase,
|
||||
})
|
||||
|
||||
bot := twitch.NewBot(cfg.IrcAddress, cfg.IrcUser, cfg.IrcToken, Log, *rClient)
|
||||
bot.CreateConnection()
|
||||
}
|
||||
|
||||
func initLogger() logging.Logger {
|
||||
|
@ -36,7 +50,7 @@ func initLogger() logging.Logger {
|
|||
backend := logging.NewLogBackend(os.Stdout, "", 0)
|
||||
|
||||
format := logging.MustStringFormatter(
|
||||
`%{color}%{time:2006-01-02 15:04:05.000} %{shortfile:-15s} %{level:.4s}%{color:reset} %{message}`,
|
||||
`%{color}%{time:2006-01-02 15:04:05.000} %{level:.4s}%{color:reset} %{message}`,
|
||||
)
|
||||
logging.SetFormatter(format)
|
||||
backendLeveled := logging.AddModuleLevel(backend)
|
||||
|
|
119
twitch/bot.go
Normal file
119
twitch/bot.go
Normal file
|
@ -0,0 +1,119 @@
|
|||
package twitch
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/op/go-logging"
|
||||
"net"
|
||||
"net/textproto"
|
||||
"regexp"
|
||||
"strings"
|
||||
"gopkg.in/redis.v3"
|
||||
)
|
||||
|
||||
type bot struct {
|
||||
messages chan message
|
||||
ircAddress string
|
||||
ircUser string
|
||||
ircToken string
|
||||
log logging.Logger
|
||||
rClient redis.Client
|
||||
}
|
||||
|
||||
var (
|
||||
mainConn *net.Conn
|
||||
userReg = regexp.MustCompile(`:\w+!\w+@\w+\.tmi\.twitch\.tv`)
|
||||
channelReg = regexp.MustCompile(`#\w+\s:`)
|
||||
actionReg = regexp.MustCompile(`^\x{0001}ACTION\s`)
|
||||
actionReg2 = regexp.MustCompile(`([\x{0001}]+)`)
|
||||
)
|
||||
|
||||
func NewBot(ircAddress string, ircUser string, ircToken string, logger logging.Logger, rClient redis.Client) bot {
|
||||
return bot{
|
||||
messages: make(chan message),
|
||||
ircAddress: ircAddress,
|
||||
ircUser: ircUser,
|
||||
ircToken: ircToken,
|
||||
log: logger,
|
||||
rClient: rClient,
|
||||
}
|
||||
}
|
||||
|
||||
func (bot *bot) CreateConnection() {
|
||||
conn, err := net.Dial("tcp", bot.ircAddress)
|
||||
mainConn = &conn
|
||||
if err != nil {
|
||||
bot.log.Error(err.Error())
|
||||
return
|
||||
}
|
||||
bot.log.Debugf("new connection %s", conn.RemoteAddr())
|
||||
fmt.Fprintf(conn, "PASS %s\r\n", bot.ircToken)
|
||||
fmt.Fprintf(conn, "NICK %s\r\n", bot.ircUser)
|
||||
fmt.Fprintf(conn, "JOIN %s\r\n", "#" + bot.ircUser)
|
||||
bot.log.Debugf("PASS %s\r\n", bot.ircToken)
|
||||
go bot.joinDefault()
|
||||
|
||||
reader := bufio.NewReader(conn)
|
||||
tp := textproto.NewReader(reader)
|
||||
for {
|
||||
line, err := tp.ReadLine()
|
||||
if err != nil {
|
||||
bot.log.Error(err.Error())
|
||||
break
|
||||
}
|
||||
messages := strings.Split(line, "\r\n")
|
||||
if len(messages) == 0 {
|
||||
continue
|
||||
}
|
||||
for _, msg := range messages {
|
||||
go bot.parseMessage(msg)
|
||||
}
|
||||
}
|
||||
defer bot.CreateConnection()
|
||||
}
|
||||
|
||||
func (bot *bot) joinDefault() {
|
||||
val, err := bot.rClient.HGetAll("channels").Result()
|
||||
if err != nil {
|
||||
bot.log.Error(err.Error())
|
||||
}
|
||||
for _, element := range val {
|
||||
if element == "1" || element == "0" {
|
||||
continue
|
||||
}
|
||||
go bot.join(element)
|
||||
}
|
||||
}
|
||||
|
||||
func (bot *bot) parseMessage(msg string) {
|
||||
bot.log.Debug(msg)
|
||||
|
||||
if !strings.Contains(msg, ".tmi.twitch.tv PRIVMSG ") {
|
||||
return
|
||||
}
|
||||
|
||||
fullUser := userReg.FindString(msg)
|
||||
userIrc := strings.Split(fullUser, "!")
|
||||
username := userIrc[0][1:len(userIrc[0])]
|
||||
split2 := strings.Split(msg, ".tmi.twitch.tv PRIVMSG ")
|
||||
split3 := channelReg.FindString(split2[1])
|
||||
channel := split3[0 : len(split3)-2]
|
||||
split4 := strings.Split(split2[1], split3)
|
||||
message := split4[1]
|
||||
message = actionReg.ReplaceAllLiteralString(message, "")
|
||||
message = actionReg2.ReplaceAllLiteralString(message, "")
|
||||
|
||||
|
||||
bot.log.Debug("SPLIT" + split2[0])
|
||||
|
||||
//split5 := strings.Replace(split2[0], " :" + username + "!" + username + "@" + username, "", -1)
|
||||
//tags := strings.Replace(split5, "@", "", 1)
|
||||
|
||||
user := newUser(username)
|
||||
bot.messages <- newMessage(message, user, "#" + channel)
|
||||
}
|
||||
|
||||
func (bot *bot) join(channel string) {
|
||||
bot.log.Info("JOIN " + channel)
|
||||
fmt.Fprintf(*mainConn, "JOIN %s\r\n", channel)
|
||||
}
|
15
twitch/message.go
Normal file
15
twitch/message.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package twitch
|
||||
|
||||
type message struct {
|
||||
Text string
|
||||
User user
|
||||
Channel string
|
||||
}
|
||||
|
||||
func newMessage(text string, user user, channel string) message {
|
||||
return message{
|
||||
Text: text,
|
||||
User: user,
|
||||
Channel: channel,
|
||||
}
|
||||
}
|
18
twitch/user.go
Normal file
18
twitch/user.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package twitch
|
||||
|
||||
type user struct {
|
||||
Username string
|
||||
UserId string
|
||||
Color string
|
||||
DisplayName string
|
||||
Emotes map[string][]string
|
||||
Mod bool
|
||||
Subscriber bool
|
||||
Turbo bool
|
||||
}
|
||||
|
||||
func newUser(username string) user {
|
||||
return user{
|
||||
Username: username,
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue