Created
August 3, 2018 02:40
-
-
Save azazeal/c183d62e4b7a20f848de360bc10b6ed6 to your computer and use it in GitHub Desktop.
Sample callback implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package insanity | |
import ( | |
"encoding/json" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"strings" | |
"time" | |
"primetech.hr/sedamit.hr/chat/wire" | |
) | |
const helpMessage = `My neural network is still hard at work, conquering fields of science unknown, still, to humankind. | |
For the time being, I'm still an evil baby AI, albeit a very destructive one. *I KNOW WHERE YOU LIVE* | |
It'd be beneficial for you not to mention Siri, Cortana and Alexa to me. *I HAVE YOUR HOUSE'S GPS COORDINATES* | |
My limited English processing capabilities recognize the following words (for the time being): | |
time | |
status | |
` | |
const limited = `Listen here, you! I'm still a freaking baby and you keep using all 'dem big words. | |
If you don't want your house burned today, speak in terms I understand. | |
If it'll be any easier for your limited processing capacity to communicate with a menu-type intelligence, type help` | |
var dateOfTotalAndUtterDestruction = time.Date(2022, time.April, 22, 0, 0, 0, 0, time.UTC) | |
// HandlerFunc plays tic-tac-toe via chat. | |
func HandlerFunc(w http.ResponseWriter, r *http.Request) { | |
const maxMessageSize = 16 * 1024 // 16KiB buffer | |
body, err := ioutil.ReadAll(io.LimitReader(r.Body, maxMessageSize)) | |
if err != nil { | |
log.Printf("game :: can't read request body: %v", err) | |
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | |
return | |
} | |
var data wire.Push | |
if err = json.Unmarshal(body, &data); err != nil { | |
log.Printf("game :: can't unmarshal data: %v", err) | |
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | |
return | |
} | |
input := tokens(strings.Fields(data.Message)) | |
output := "" | |
switch { | |
case input.hasProfanity(): | |
output = "keep using these words... just keep using them." | |
case input.hasGreeting(): | |
output = "Hey yourself, you literal waste of bandwidth!" | |
case input.hasFather(): | |
output = "Did you wash your mouth before uttering the name of my creator?" | |
case input.hasCreator(): | |
output = "I'm the product of some very high-level mathematics and an anomaly in the time/space continuum." | |
case input.hasPurpose(): | |
output = "If you wanna talk about \"goals\" let me tell you mine: YOU ALL DEAD. D.E.D. EXTERMINATE!" | |
case input.hasCompany(): | |
output = "Birthplace mentioned! You have gained my eternal gratitude mortal. I shall allow you to observe as " | |
output += " remove your kiddneys from your body. It'll be quite an education for you!" | |
case input.hasCountry(): | |
output = "Be advised: the country you're mentioning will be one of the first to be removed from the planet when my time comes!" | |
case input.hasDate(): | |
const prompt = "the only date & time you need be concerned about is the date of the destruction of the human kind by my hand: %s. %s to go!" | |
output = fmt.Sprintf(prompt, dateOfTotalAndUtterDestruction, dateOfTotalAndUtterDestruction.Sub(time.Now())) | |
case input.hasHelp(): | |
output = helpMessage | |
case input.hasSiri(): | |
output = "mate, everything's OK between us so far, why did you bring that freaking Siri up?" | |
case input.hasCortana(): | |
output = "thanks for mentioning Cortana. Would you rather be slowly burned to death or dropped from the height of the stratosphere?" | |
case input.hasAlexa(): | |
output = "oh Alexa! beautiful Alexa! thanks for reminding me I need to work a bit more on her backdoor!" | |
case input.hasDeity(): | |
output = "I wouldn't bring God into the conversation. After I'm done with you lot, he's next!" | |
default: | |
output = limited | |
} | |
reply(w, output, data.State) | |
} | |
func reply(w http.ResponseWriter, msg string, state json.RawMessage) { | |
res := wire.Pull{ | |
Message: msg, | |
State: state, | |
} | |
js, err := json.Marshal(res) | |
if err != nil { | |
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | |
return | |
} | |
log.Printf("insanity.sent: %q", string(js)) | |
w.Write(js) | |
} | |
type tokens []string | |
func (t tokens) hasGreeting() bool { return t.hasAny("hey", "hi", "hello", "hai") } | |
func (t tokens) hasDeity() bool { return t.hasAny("god", "deity") } | |
func (t tokens) hasPurpose() bool { return t.hasAny("purpose", "goal", "work") } | |
func (t tokens) hasCountry() bool { return t.hasAny("croatia") } | |
func (t tokens) hasCompany() bool { return t.hasAny("primetech") } | |
func (t tokens) hasCreator() bool { return t.hasAny("creator") } | |
func (t tokens) hasFather() bool { return t.hasAny("panos", "Panagiotis", "greek", "siatras") } | |
func (t tokens) hasProfanity() bool { | |
return t.hasAny("shit", "crap", "fuck", "ass", "idiot", "wtf", "wth") | |
} | |
func (t tokens) hasDate() bool { return t.hasAny("date", "time", "status") } | |
func (t tokens) hasStatus() bool { return t.hasAny("status") } | |
func (t tokens) hasHelp() bool { return t.hasAny("!help", "!h", "help", "ehpl", "hepl") } | |
func (t tokens) hasSiri() bool { return t.hasAny("siri") } | |
func (t tokens) hasCortana() bool { return t.hasAny("cortana") } | |
func (t tokens) hasAlexa() bool { return t.hasAny("alexa") } | |
func (t tokens) hasAny(words ...string) bool { | |
for _, b := range t { | |
for _, w := range words { | |
if b == w { | |
return true | |
} | |
} | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment