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
import "github.com/hamcha/clessy/tg"
import (
"math/rand"
"strings"
var nomifamosi = []string{
"Voltaire", "Lucio Dalla", "Newton", "Cartesio", "Socrate", "Nietzsche",
"Pascoli", "Platone",
"github.com/hamcha/clessy/tg"
)
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{
"lo scassapalle", "l'archibugio", "il minchione", "il saputello",
var dantes_manomi = []string{
"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{
"scusa se faccio _", "non per fare il _",
}
var dantes_tipibrutti = []string{
"lo scassapalle", "il minchione", "il saputello", "lo stronzo",
}
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]
}
/*
MA TU SEI FUORI
id := update.MessageID
if update.ReplyTo != nil {
id = update.ReplyTo.MessageID
}
ATTENZIONE RAGAZZI, E' ARRIVATO <NOME FAMOSO>
broker.SendTextMessage(update.Chat, frase, &id)
return
}
Scusa se faccio <roba>, ma hai detto una stronzata
*/
}

View file

@ -20,7 +20,7 @@ type Macro struct {
var macropath *string
var macros map[string]Macro
func initmacro() {
func macro_init() {
macros = make(map[string]Macro)
file, err := os.Open(*macropath)
if err != nil {
@ -35,7 +35,7 @@ func initmacro() {
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") {
parts := strings.SplitN(*(update.Text), " ", 3)
switch len(parts) {

View file

@ -18,51 +18,54 @@ type Mod struct {
var mods = map[string]Mod{
"metafora": {
OnMessage: metafora,
OnMessage: metafora_message,
},
"dantes": {
OnMessage: dantes_message,
},
"viaggi": {
OnInit: initviaggi,
OnMessage: viaggi,
OnInit: viaggi_init,
OnMessage: viaggi_message,
},
"meme": {
OnInit: initmeme,
OnMessage: memegen,
OnInit: meme_init,
OnMessage: meme_message,
},
"unsplash": {
OnInit: initunsplash,
OnMessage: unsplash,
OnInit: unsplash_init,
OnMessage: unsplash_message,
},
"macro": {
OnInit: initmacro,
OnMessage: macro,
OnInit: macro_init,
OnMessage: macro_message,
},
"snapchat": {
OnInit: initsnapchat,
OnMessage: snapchat,
OnInit: snapchat_init,
OnMessage: snapchat_message,
},
"proverbio": {
OnInit: initproverbio,
OnMessage: proverbio,
OnInit: proverbio_init,
OnMessage: proverbio_message,
},
"talk": {
OnInit: inittalk,
OnMessage: talk,
OnInit: talk_init,
OnMessage: talk_message,
},
"stt": {
OnInit: initstt,
OnMessage: stt,
OnInit: stt_init,
OnMessage: stt_message,
},
"remind": {
OnInit: initremind,
OnMessage: remind,
OnInit: remind_init,
OnMessage: remind_message,
},
"search": {
OnInit: initsearch,
OnMessage: search,
OnInit: search_init,
OnMessage: search_message,
},
"oroscopo": {
OnInit: initoroscopo,
OnMessage: oroscopo,
OnInit: oroscopo_init,
OnMessage: oroscopo_message,
},
}
@ -95,7 +98,20 @@ func isCommand(update tg.APIMessage, cmdname string) bool {
}
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

View file

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

View file

@ -29,7 +29,7 @@ var metaobjects = []string{
"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") {
broker.SendTextMessage(update.Chat, metaforaAPI(), nil)
return

View file

@ -15,7 +15,7 @@ import (
var orogen *gomarkov.Chain
func initoroscopo() {
func oroscopo_init() {
counter := 0
orogen = gomarkov.NewChain()
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)
}
func oroscopo(broker *tg.Broker, update tg.APIMessage) {
func oroscopo_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "oroscopo") {
var err error
var txt string

View file

@ -18,7 +18,7 @@ type ProverbioData struct {
var proverbipairs ProverbioData
func initproverbio() {
func proverbio_init() {
data, err := ioutil.ReadFile(*proverbi)
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))
}
func proverbio(broker *tg.Broker, update tg.APIMessage) {
func proverbio_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "proverbio") {
n := rand.Intn(len(proverbipairs.Inizio))
m := rand.Intn(len(proverbipairs.Fine))

View file

@ -31,7 +31,7 @@ var reminders map[string]Reminder
const ReminderMaxDuration = time.Hour * 24 * 30 * 3
func initremind() {
func remind_init() {
reminders = make(map[string]Reminder)
file, err := os.Open(*remindpath)
if err != nil {
@ -49,7 +49,7 @@ func initremind() {
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") {
// Supported formats:
// Xs/m/h/d => in X seconds/minutes/hours/days

View file

@ -44,7 +44,7 @@ type SearchResultData struct {
const MAX_SEARCH_ALTS = 2
func initsearch() {
func search_init() {
if strings.HasPrefix(*gapikey, "@") {
data, err := ioutil.ReadFile((*gapikey)[1:])
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") {
parts := strings.SplitN(*(update.Text), " ", 2)
if len(parts) < 2 {

View file

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

View file

@ -16,7 +16,7 @@ import (
var sttClient *speech.Client
var sttCtx context.Context
func initstt() {
func stt_init() {
sttCtx = context.Background()
var err error
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") {
// Make replies work
if update.ReplyTo != nil && update.ReplyTo.Voice != nil {

View file

@ -52,7 +52,7 @@ type QResponse struct {
const talkBaseURL = "https://api.api.ai/v1"
func inittalk() {
func talk_init() {
if strings.HasPrefix(*talktoken, "@") {
data, err := ioutil.ReadFile((*talktoken)[1:])
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
if update.Text == nil {
return

View file

@ -24,7 +24,7 @@ const unsplashUrl = "https://source.unsplash.com/random/1280x720"
var quoteFontData draw2d.FontData
func initunsplash() {
func unsplash_init() {
fontfile, err := os.Open(*gillmt)
assert(err)
defer fontfile.Close()
@ -51,7 +51,7 @@ func stripUnreadable(r rune) rune {
return r
}
func unsplash(broker *tg.Broker, update tg.APIMessage) {
func unsplash_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "unsplash") {
text := ""
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
func initviaggi() {
func viaggi_init() {
reg = regexp.MustCompile("([^-]+) -> (.+)")
}
func viaggi(broker *tg.Broker, update tg.APIMessage) {
func viaggi_message(broker *tg.Broker, update tg.APIMessage) {
if isCommand(update, "viaggi") {
usage := func() {
broker.SendTextMessage(update.Chat, "Formato: /viaggi <i>&lt;PARTENZA&gt;</i> -> <i>&lt;DESTINAZIONE&gt;</i>", &update.MessageID)