MEGAREFACTOR + dantes

This commit is contained in:
Hamcha 2018-05-24 17:48:35 +02:00
parent 8b8228c0f9
commit b29294fcbf
Signed by: hamcha
GPG key ID: A40413D21021EAEE
14 changed files with 117 additions and 60 deletions

View file

@ -1,24 +1,65 @@
package main package main
import "github.com/hamcha/clessy/tg" import (
"math/rand"
"strings"
var nomifamosi = []string{ "github.com/hamcha/clessy/tg"
"Voltaire", "Lucio Dalla", "Newton", "Cartesio", "Socrate", "Nietzsche", )
"Pascoli", "Platone",
var dantes_frasiacaso = []string{
"MA TU SEI FUORI", "scusa ma chi cazzo se ne frega", "snì", "io non discuto più, derido e ignoro", "MA CHI SEIIIIIII", "Non commento",
} }
var tipibrutti = []string{ var dantes_manomi = []string{
"lo scassapalle", "l'archibugio", "il minchione", "il saputello", "Ma chi ti credi, _?", "ATTENZIONE RAGAZZI, È ARRIVATO _", "Eh allora vallo a dire a _",
} }
func dantes(broker *tg.Broker, update tg.APIMessage) { var dantes_nomifamosi = []string{
"Voltaire", "Lucio Dalla", "Newton", "Cartesio", "Socrate", "Nietzsche", "Pascoli", "Platone", "Ricky Martin",
} }
/* var dantes_scusa = []string{
MA TU SEI FUORI "scusa se faccio _", "non per fare il _",
}
ATTENZIONE RAGAZZI, E' ARRIVATO <NOME FAMOSO> var dantes_tipibrutti = []string{
"lo scassapalle", "il minchione", "il saputello", "lo stronzo",
}
Scusa se faccio <roba>, ma hai detto una stronzata var dantes_followup = []string{
*/ " ma hai detto una stronzata", " però cristo dai", ", ma ti sei letto?", ", ma cosa cazzo dici",
}
func dantes_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "dantes") {
frase := ""
switch rand.Intn(3) {
case 0:
// Frasi a caso
fraseid := rand.Intn(len(dantes_frasiacaso))
frase = dantes_frasiacaso[fraseid]
case 1:
// Ma nomi
incipitid := rand.Intn(len(dantes_manomi))
nomeid := rand.Intn(len(dantes_nomifamosi))
frase = strings.Replace(dantes_manomi[incipitid], "_", dantes_nomifamosi[nomeid], 1)
case 2:
// Brutta persona
scusaid := rand.Intn(len(dantes_scusa))
tipoid := rand.Intn(len(dantes_tipibrutti))
followid := rand.Intn(len(dantes_followup))
frase = strings.Replace(dantes_scusa[scusaid], "_", dantes_tipibrutti[tipoid], 1) + dantes_followup[followid]
}
id := update.MessageID
if update.ReplyTo != nil {
id = update.ReplyTo.MessageID
}
broker.SendTextMessage(update.Chat, frase, &id)
return
}
}

View file

@ -20,7 +20,7 @@ type Macro struct {
var macropath *string var macropath *string
var macros map[string]Macro var macros map[string]Macro
func initmacro() { func macro_init() {
macros = make(map[string]Macro) macros = make(map[string]Macro)
file, err := os.Open(*macropath) file, err := os.Open(*macropath)
if err != nil { if err != nil {
@ -35,7 +35,7 @@ func initmacro() {
log.Printf("[macro] Loaded %d macros from %s\n", len(macros), *macropath) log.Printf("[macro] Loaded %d macros from %s\n", len(macros), *macropath)
} }
func macro(broker *tg.Broker, update tg.APIMessage) { func macro_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "macro") { if isCommand(update, "macro") {
parts := strings.SplitN(*(update.Text), " ", 3) parts := strings.SplitN(*(update.Text), " ", 3)
switch len(parts) { switch len(parts) {

View file

@ -18,51 +18,54 @@ type Mod struct {
var mods = map[string]Mod{ var mods = map[string]Mod{
"metafora": { "metafora": {
OnMessage: metafora, OnMessage: metafora_message,
},
"dantes": {
OnMessage: dantes_message,
}, },
"viaggi": { "viaggi": {
OnInit: initviaggi, OnInit: viaggi_init,
OnMessage: viaggi, OnMessage: viaggi_message,
}, },
"meme": { "meme": {
OnInit: initmeme, OnInit: meme_init,
OnMessage: memegen, OnMessage: meme_message,
}, },
"unsplash": { "unsplash": {
OnInit: initunsplash, OnInit: unsplash_init,
OnMessage: unsplash, OnMessage: unsplash_message,
}, },
"macro": { "macro": {
OnInit: initmacro, OnInit: macro_init,
OnMessage: macro, OnMessage: macro_message,
}, },
"snapchat": { "snapchat": {
OnInit: initsnapchat, OnInit: snapchat_init,
OnMessage: snapchat, OnMessage: snapchat_message,
}, },
"proverbio": { "proverbio": {
OnInit: initproverbio, OnInit: proverbio_init,
OnMessage: proverbio, OnMessage: proverbio_message,
}, },
"talk": { "talk": {
OnInit: inittalk, OnInit: talk_init,
OnMessage: talk, OnMessage: talk_message,
}, },
"stt": { "stt": {
OnInit: initstt, OnInit: stt_init,
OnMessage: stt, OnMessage: stt_message,
}, },
"remind": { "remind": {
OnInit: initremind, OnInit: remind_init,
OnMessage: remind, OnMessage: remind_message,
}, },
"search": { "search": {
OnInit: initsearch, OnInit: search_init,
OnMessage: search, OnMessage: search_message,
}, },
"oroscopo": { "oroscopo": {
OnInit: initoroscopo, OnInit: oroscopo_init,
OnMessage: oroscopo, OnMessage: oroscopo_message,
}, },
} }
@ -95,7 +98,20 @@ func isCommand(update tg.APIMessage, cmdname string) bool {
} }
text := *(update.Text) text := *(update.Text)
return strings.HasPrefix(text, "/"+cmdname+"@"+*botname) || (strings.HasPrefix(text, "/"+cmdname) && !strings.Contains(text, "@"))
// Check without bot suffix
prefix := "/" + cmdname
if text == prefix || strings.HasPrefix(text, prefix+" ") {
return true
}
// Check with bot suffix
prefix = "/" + cmdname + "@" + *botname
if text == prefix || strings.HasPrefix(text, prefix+" ") {
return true
}
return false
} }
var broker *tg.Broker var broker *tg.Broker

View file

@ -20,7 +20,7 @@ import (
var memeFontData draw2d.FontData var memeFontData draw2d.FontData
func initmeme() { func meme_init() {
fontfile, err := os.Open(*impact) fontfile, err := os.Open(*impact)
assert(err) assert(err)
defer fontfile.Close() defer fontfile.Close()
@ -37,7 +37,7 @@ func initmeme() {
log.Println("[meme] Loaded!") log.Println("[meme] Loaded!")
} }
func memegen(broker *tg.Broker, update tg.APIMessage) { func meme_message(broker *tg.Broker, update tg.APIMessage) {
// Make replies work // Make replies work
if update.ReplyTo != nil && update.Text != nil && update.ReplyTo.Photo != nil { if update.ReplyTo != nil && update.Text != nil && update.ReplyTo.Photo != nil {
update.Photo = update.ReplyTo.Photo update.Photo = update.ReplyTo.Photo

View file

@ -29,7 +29,7 @@ var metaobjects = []string{
"la manovella", "il pennello", "l'asta", "il cacciavite", "lo spazzolino", "la manovella", "il pennello", "l'asta", "il cacciavite", "lo spazzolino",
} }
func metafora(broker *tg.Broker, update tg.APIMessage) { func metafora_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "metafora") { if isCommand(update, "metafora") {
broker.SendTextMessage(update.Chat, metaforaAPI(), nil) broker.SendTextMessage(update.Chat, metaforaAPI(), nil)
return return

View file

@ -15,7 +15,7 @@ import (
var orogen *gomarkov.Chain var orogen *gomarkov.Chain
func initoroscopo() { func oroscopo_init() {
counter := 0 counter := 0
orogen = gomarkov.NewChain() orogen = gomarkov.NewChain()
filepath.Walk(*oropath, func(path string, info os.FileInfo, err error) error { filepath.Walk(*oropath, func(path string, info os.FileInfo, err error) error {
@ -37,7 +37,7 @@ func initoroscopo() {
log.Printf("[oroscopo] Loaded corpus from %d files\n", counter) log.Printf("[oroscopo] Loaded corpus from %d files\n", counter)
} }
func oroscopo(broker *tg.Broker, update tg.APIMessage) { func oroscopo_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "oroscopo") { if isCommand(update, "oroscopo") {
var err error var err error
var txt string var txt string

View file

@ -18,7 +18,7 @@ type ProverbioData struct {
var proverbipairs ProverbioData var proverbipairs ProverbioData
func initproverbio() { func proverbio_init() {
data, err := ioutil.ReadFile(*proverbi) data, err := ioutil.ReadFile(*proverbi)
assert(err) assert(err)
@ -36,7 +36,7 @@ func initproverbio() {
log.Printf("[proverbio] Loaded %d pairs (%d combinations!)\n", len(proverbipairs.Inizio), len(proverbipairs.Inizio)*len(proverbipairs.Fine)) log.Printf("[proverbio] Loaded %d pairs (%d combinations!)\n", len(proverbipairs.Inizio), len(proverbipairs.Inizio)*len(proverbipairs.Fine))
} }
func proverbio(broker *tg.Broker, update tg.APIMessage) { func proverbio_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "proverbio") { if isCommand(update, "proverbio") {
n := rand.Intn(len(proverbipairs.Inizio)) n := rand.Intn(len(proverbipairs.Inizio))
m := rand.Intn(len(proverbipairs.Fine)) m := rand.Intn(len(proverbipairs.Fine))

View file

@ -31,7 +31,7 @@ var reminders map[string]Reminder
const ReminderMaxDuration = time.Hour * 24 * 30 * 3 const ReminderMaxDuration = time.Hour * 24 * 30 * 3
func initremind() { func remind_init() {
reminders = make(map[string]Reminder) reminders = make(map[string]Reminder)
file, err := os.Open(*remindpath) file, err := os.Open(*remindpath)
if err != nil { if err != nil {
@ -49,7 +49,7 @@ func initremind() {
log.Printf("[remind] Loaded %d pending reminders from %s\n", len(reminders), *remindpath) log.Printf("[remind] Loaded %d pending reminders from %s\n", len(reminders), *remindpath)
} }
func remind(broker *tg.Broker, update tg.APIMessage) { func remind_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "ricordami") { if isCommand(update, "ricordami") {
// Supported formats: // Supported formats:
// Xs/m/h/d => in X seconds/minutes/hours/days // Xs/m/h/d => in X seconds/minutes/hours/days

View file

@ -44,7 +44,7 @@ type SearchResultData struct {
const MAX_SEARCH_ALTS = 2 const MAX_SEARCH_ALTS = 2
func initsearch() { func search_init() {
if strings.HasPrefix(*gapikey, "@") { if strings.HasPrefix(*gapikey, "@") {
data, err := ioutil.ReadFile((*gapikey)[1:]) data, err := ioutil.ReadFile((*gapikey)[1:])
if err != nil { if err != nil {
@ -57,7 +57,7 @@ func initsearch() {
} }
} }
func search(broker *tg.Broker, update tg.APIMessage) { func search_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "search") { if isCommand(update, "search") {
parts := strings.SplitN(*(update.Text), " ", 2) parts := strings.SplitN(*(update.Text), " ", 2)
if len(parts) < 2 { if len(parts) < 2 {

View file

@ -25,7 +25,7 @@ import (
var snapFontData draw2d.FontData var snapFontData draw2d.FontData
func initsnapchat() { func snapchat_init() {
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
fontfile, err := os.Open(*sourcesans) fontfile, err := os.Open(*sourcesans)
@ -44,7 +44,7 @@ func initsnapchat() {
log.Println("[snapchat] Loaded!") log.Println("[snapchat] Loaded!")
} }
func snapchat(broker *tg.Broker, update tg.APIMessage) { func snapchat_message(broker *tg.Broker, update tg.APIMessage) {
// Make replies work // Make replies work
if update.ReplyTo != nil && update.Text != nil && update.ReplyTo.Photo != nil { if update.ReplyTo != nil && update.Text != nil && update.ReplyTo.Photo != nil {
update.Photo = update.ReplyTo.Photo update.Photo = update.ReplyTo.Photo

View file

@ -16,7 +16,7 @@ import (
var sttClient *speech.Client var sttClient *speech.Client
var sttCtx context.Context var sttCtx context.Context
func initstt() { func stt_init() {
sttCtx = context.Background() sttCtx = context.Background()
var err error var err error
sttClient, err = speech.NewClient(sttCtx, option.WithServiceAccountFile(*gapifile)) sttClient, err = speech.NewClient(sttCtx, option.WithServiceAccountFile(*gapifile))
@ -25,7 +25,7 @@ func initstt() {
} }
} }
func stt(broker *tg.Broker, update tg.APIMessage) { func stt_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "stt") { if isCommand(update, "stt") {
// Make replies work // Make replies work
if update.ReplyTo != nil && update.ReplyTo.Voice != nil { if update.ReplyTo != nil && update.ReplyTo.Voice != nil {

View file

@ -52,7 +52,7 @@ type QResponse struct {
const talkBaseURL = "https://api.api.ai/v1" const talkBaseURL = "https://api.api.ai/v1"
func inittalk() { func talk_init() {
if strings.HasPrefix(*talktoken, "@") { if strings.HasPrefix(*talktoken, "@") {
data, err := ioutil.ReadFile((*talktoken)[1:]) data, err := ioutil.ReadFile((*talktoken)[1:])
if err != nil { if err != nil {
@ -65,7 +65,7 @@ func inittalk() {
} }
} }
func talk(broker *tg.Broker, update tg.APIMessage) { func talk_message(broker *tg.Broker, update tg.APIMessage) {
// Must be a text message // Must be a text message
if update.Text == nil { if update.Text == nil {
return return

View file

@ -24,7 +24,7 @@ const unsplashUrl = "https://source.unsplash.com/random/1280x720"
var quoteFontData draw2d.FontData var quoteFontData draw2d.FontData
func initunsplash() { func unsplash_init() {
fontfile, err := os.Open(*gillmt) fontfile, err := os.Open(*gillmt)
assert(err) assert(err)
defer fontfile.Close() defer fontfile.Close()
@ -51,7 +51,7 @@ func stripUnreadable(r rune) rune {
return r return r
} }
func unsplash(broker *tg.Broker, update tg.APIMessage) { func unsplash_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "unsplash") { if isCommand(update, "unsplash") {
text := "" text := ""
user := update.User user := update.User

View file

@ -17,11 +17,11 @@ const viaggiurl = "http://free.rome2rio.com/api/1.2/json/Search?key=X5JMLHNc&lan
var reg *regexp.Regexp var reg *regexp.Regexp
func initviaggi() { func viaggi_init() {
reg = regexp.MustCompile("([^-]+) -> (.+)") reg = regexp.MustCompile("([^-]+) -> (.+)")
} }
func viaggi(broker *tg.Broker, update tg.APIMessage) { func viaggi_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "viaggi") { if isCommand(update, "viaggi") {
usage := func() { usage := func() {
broker.SendTextMessage(update.Chat, "Formato: /viaggi <i>&lt;PARTENZA&gt;</i> -> <i>&lt;DESTINAZIONE&gt;</i>", &update.MessageID) broker.SendTextMessage(update.Chat, "Formato: /viaggi <i>&lt;PARTENZA&gt;</i> -> <i>&lt;DESTINAZIONE&gt;</i>", &update.MessageID)