2017-06-17 20:20:06 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package reaction
|
|
|
|
|
|
|
|
import (
|
2019-09-27 14:39:43 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2017-06-17 20:20:06 +00:00
|
|
|
"math/rand"
|
|
|
|
|
2019-09-27 14:39:43 +00:00
|
|
|
"github.com/chrissexton/sentiment"
|
2017-06-17 20:20:06 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
2017-08-30 17:41:58 +00:00
|
|
|
"github.com/velour/catbase/config"
|
2017-06-17 20:20:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ReactionPlugin struct {
|
2019-05-27 23:21:53 +00:00
|
|
|
bot bot.Bot
|
|
|
|
config *config.Config
|
2019-09-27 14:39:43 +00:00
|
|
|
|
|
|
|
model sentiment.Models
|
2017-06-17 20:20:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 19:41:38 +00:00
|
|
|
func New(b bot.Bot) *ReactionPlugin {
|
2019-09-27 14:39:43 +00:00
|
|
|
model, err := sentiment.Restore()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Couldn't restore sentiment model")
|
|
|
|
}
|
2019-02-05 19:41:38 +00:00
|
|
|
rp := &ReactionPlugin{
|
2019-05-27 23:21:53 +00:00
|
|
|
bot: b,
|
|
|
|
config: b.Config(),
|
2019-09-27 14:39:43 +00:00
|
|
|
model: model,
|
2017-06-17 20:20:06 +00:00
|
|
|
}
|
2019-02-05 19:41:38 +00:00
|
|
|
b.Register(rp, bot.Message, rp.message)
|
|
|
|
return rp
|
2017-06-17 20:20:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *ReactionPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
|
|
|
chance := p.config.GetFloat64("Reaction.GeneralChance", 0.01)
|
2017-08-30 18:44:04 +00:00
|
|
|
if rand.Float64() < chance {
|
2019-09-27 14:39:43 +00:00
|
|
|
analysis := p.model.SentimentAnalysis(message.Body, sentiment.English)
|
2017-08-30 18:44:04 +00:00
|
|
|
|
2019-09-27 14:39:43 +00:00
|
|
|
log.Debug().
|
|
|
|
Uint8("score", analysis.Score).
|
|
|
|
Str("body", message.Body).
|
|
|
|
Msg("sentiment of statement")
|
2017-08-30 18:44:04 +00:00
|
|
|
|
2019-09-27 14:39:43 +00:00
|
|
|
var reactions []string
|
|
|
|
if analysis.Score > 0 {
|
|
|
|
reactions = p.config.GetArray("Reaction.PositiveReactions", []string{})
|
2017-08-30 18:44:04 +00:00
|
|
|
} else {
|
2019-09-27 14:39:43 +00:00
|
|
|
reactions = p.config.GetArray("Reaction.NegativeReactions", []string{})
|
2017-08-30 17:41:58 +00:00
|
|
|
}
|
2017-08-30 18:44:04 +00:00
|
|
|
|
2019-09-27 14:39:43 +00:00
|
|
|
reaction := reactions[rand.Intn(len(reactions))]
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
p.bot.Send(c, bot.Reaction, message.Channel, reaction, message)
|
2017-08-30 17:41:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 20:20:06 +00:00
|
|
|
return false
|
|
|
|
}
|