Skip to content
Extraits de code Groupes Projets
Valider 7cb17e01 rédigé par Alexandre Morignot's avatar Alexandre Morignot
Parcourir les fichiers

Slack support multiple channels

parent 2a3fc34f
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -14,20 +14,23 @@ import (
type Bot interface {
Collection
ParseLine(author string, source string, line string, contents chan *site.Content) error
ParseLine(author string, line string, contents chan *site.Content) error
}
type PlayBot struct {
Collection
Source string
}
func NewPlayBot(source string, db Db, readers []site.Reader) *PlayBot {
return &PlayBot{
Collection: &PlayBotCollection{
Source: source,
Db: db,
Readers: readers,
},
Source: source,
}
}
......@@ -48,7 +51,7 @@ func (*PlayBot) ExtractTags(line string) []string {
return tags
}
func (pb *PlayBot) MakePost(author string, source string, url string, tags []string) *Post {
func (pb *PlayBot) MakePost(author string, url string, tags []string) *Post {
content, err := pb.GetByUrl(url)
if err != nil {
log.Print(err)
......@@ -59,24 +62,23 @@ func (pb *PlayBot) MakePost(author string, source string, url string, tags []str
post := &Post{
Date: time.Now(),
Author: author,
Source: source,
Source: pb.Source,
Content: content,
}
return post
}
func (pb *PlayBot) ParseLine(author string, source string, line string, contents chan *site.Content) error {
func (pb *PlayBot) ParseLine(author string, line string, contents chan *site.Content) error {
tags := pb.ExtractTags(line)
urls := xurls.Strict.FindAllString(line, -1)
var wg sync.WaitGroup
for _, url := range urls {
log.Print(url)
wg.Add(1)
go func(url string) {
defer wg.Done()
post := pb.MakePost(author, source, url, tags)
post := pb.MakePost(author, url, tags)
pb.InsertPost(post)
if post != nil {
contents <- post.Content
......
......@@ -11,7 +11,6 @@ type Collection interface {
}
type PlayBotCollection struct {
Source string
Db Db
Readers []site.Reader
}
......
package site
import (
"log"
)
type Content struct {
Author string `gorm:"column:sender"`
Duration int
......@@ -23,9 +19,7 @@ func (Content) TableName() string {
}
func (c *Content) AddTags(tags []string) {
log.Print(tags)
for _, tag := range tags {
c.Tags = append(c.Tags, Tag{Tag: tag})
log.Print(c.Tags)
}
}
......@@ -11,7 +11,7 @@ func (t *IrcTransport) connected(conn *irc.Conn, line *irc.Line) {
t.Logger.Printf("Join %s", channel)
conn.Join(channel)
bot := t.botFactory.GetBot("irc.iiens.net")
bot := t.botFactory.GetBot(t.getSourceName(channel))
t.bots[channel] = bot
}
}
......@@ -26,8 +26,7 @@ func (t *IrcTransport) privmsg(conn *irc.Conn, line *irc.Line) {
bot := t.bots[channel]
contents := make(chan *site.Content)
source := t.getSourceName(channel)
go bot.ParseLine(line.Nick, source, msg, contents)
go bot.ParseLine(line.Nick, msg, contents)
for content := range contents {
t.printNewContent(conn, content, channel)
......
......@@ -8,20 +8,17 @@ import (
func (t *SlackTransport) connected(ev *slack.ConnectedEvent) {
t.Logger.Print("Connected")
for _, channel := range t.channels {
bot := t.botFactory.GetBot(t.Name)
t.bots[channel] = bot
}
t.bot = t.botFactory.GetBot(t.Name)
}
func (t *SlackTransport) message(ev *slack.MessageEvent) {
bot, ok := t.bots[ev.Channel]
if !ok {
return
}
contents := make(chan *site.Content)
source := t.getSourceName(ev.Channel)
go t.bot.ParseLine(ev.User, source, ev.Text, contents)
go bot.ParseLine(ev.User, ev.Text, contents)
for content := range contents {
t.printNewContent(content, ev.Channel)
......
......@@ -17,7 +17,6 @@ type SlackTransport struct {
api *slack.Client
bot bot.Bot
bots map[string]bot.Bot
channels []string
botFactory bot.Factory
rtm *slack.RTM
quit chan bool
......@@ -44,9 +43,19 @@ func New(name string, config map[string]interface{}, factory bot.Factory, quit c
},
}
channels, ok := config["chans"]
for _, channel := range channels.([]interface{}) {
t.channels = append(t.channels, channel.(string))
channels, err := t.api.GetChannels(true)
if err != nil {
return nil, err
}
for _, channel := range channels {
if !channel.IsMember {
continue
}
t.Logger.Printf("Create bot for %s", channel.Name)
bot := t.botFactory.GetBot(t.getSourceName(channel.Name))
t.bots[channel.ID] = bot
}
return t, nil
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter