This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/mods/oroscopo.go

62 lines
1 KiB
Go
Raw Normal View History

2017-08-07 13:19:14 +00:00
package main
import (
"log"
"os"
"strings"
"io/ioutil"
"path/filepath"
"github.com/hamcha/clessy/tg"
2017-08-07 13:39:48 +00:00
gomarkov "github.com/simon-weber/gomarkov"
2017-08-07 13:19:14 +00:00
)
2017-08-07 13:39:48 +00:00
var orogen *gomarkov.Chain
2017-08-07 13:19:14 +00:00
func initoroscopo() {
counter := 0
2017-08-07 13:39:48 +00:00
orogen = gomarkov.NewChain()
2017-08-07 13:19:14 +00:00
filepath.Walk(*oropath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
2017-08-07 13:39:48 +00:00
orogen.Update(string(data))
2017-08-07 13:19:14 +00:00
counter++
return nil
})
log.Printf("[oroscopo] Loaded corpus from %d files\n", counter)
}
func oroscopo(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "oroscopo") {
2017-08-07 13:39:48 +00:00
var err error
var txt string
2017-08-07 13:33:58 +00:00
tries := 0
for tries < 10 {
2017-08-07 13:39:48 +00:00
txt, err = orogen.Respond("", 20, 100)
if err != nil {
txt = err.Error()
tries++
continue
}
2017-08-07 13:19:14 +00:00
idx := strings.LastIndexByte(txt, '.')
if idx < 10 {
2017-08-07 13:33:58 +00:00
tries++
2017-08-07 13:19:14 +00:00
continue
}
txt = txt[:idx]
2017-08-07 13:33:58 +00:00
break
2017-08-07 13:19:14 +00:00
}
2017-08-07 13:33:58 +00:00
broker.SendTextMessage(update.Chat, "<b>Ecco cosa dicono le stelle:</b>\n"+txt+".", &update.MessageID)
2017-08-07 13:19:14 +00:00
}
}