From 156a8601fc0665f7b261e0a8569c32dd5de446dc Mon Sep 17 00:00:00 2001 From: William Cook Date: Tue, 13 Oct 2020 22:38:22 -0700 Subject: [PATCH] Create wrapper function for sending tweet on a regex match --- main.go | 185 ++++++++++---------------------------------------------- 1 file changed, 32 insertions(+), 153 deletions(-) diff --git a/main.go b/main.go index b1e41f9..5c31746 100644 --- a/main.go +++ b/main.go @@ -112,187 +112,66 @@ func processHonk(twitterAPI *anaconda.TwitterApi, tweet anaconda.Tweet) { } if commandHonkMatch.MatchString(tweet.Text) { - log.Println("Tweet matched honk") - - if strings.Contains(tweet.Text, "RT ") { - log.Println("This is a RT dont reply to not flood") - return - } - - mention := tweet.InReplyToScreenName - if mention == "" { - mention = tweet.User.ScreenName - } - - var image []byte - var msg string if strings.Contains(tweet.Text, "capybara") { - image = getImage("capybara") - msg = fmt.Sprintf("@%s Honkbara the Planet", mention) + matchTweet(twitterAPI, tweet, "honk", "capabara", "Honkbara the planet") } else { - image = getImage("goose") - msg = fmt.Sprintf("@%s Honk the Planet #honkbot", mention) - } - - if image == nil { - image = getDefaultGoose() + matchTweet(twitterAPI, tweet, "honk", "goose", "Honk the planet") } - - sendTweet(twitterAPI, tweet.IdStr, msg, image) } if commandMeowMatch.MatchString(tweet.Text) { - log.Println("Tweet matched meow") - - if strings.Contains(tweet.Text, "RT ") { - log.Println("This is a RT dont reply to not flood") - return - } - - mention := tweet.InReplyToScreenName - if mention == "" { - mention = tweet.User.ScreenName - } - - image := getImage("cat") - if image == nil { - image = getDefaultGoose() - } - - msg := fmt.Sprintf("@%s Meow the Planet #honkbot", mention) - sendTweet(twitterAPI, tweet.IdStr, msg, image) + matchTweet(twitterAPI, tweet, "meow", "cat", "Meow the planet") } if commandPonyMatch.MatchString(tweet.Text) { - log.Println("Tweet matched pony") - - if strings.Contains(tweet.Text, "RT ") { - log.Println("This is a RT dont reply to not flood") - return - } - - mention := tweet.InReplyToScreenName - if mention == "" { - mention = tweet.User.ScreenName - } - - image := getPony() - if image == nil { - image = getDefaultGoose() - } - - msg := fmt.Sprintf("@%s #honkbot #pony", mention) - sendTweet(twitterAPI, tweet.IdStr, msg, image) + matchTweet(twitterAPI, tweet, "pony", "pony", "#pony") } if commandWoofMatch.MatchString(tweet.Text) { - log.Println("Tweet matched woof") - - if strings.Contains(tweet.Text, "RT ") { - log.Println("This is a RT dont reply to not flood") - return - } - - mention := tweet.InReplyToScreenName - if mention == "" { - mention = tweet.User.ScreenName - } - - image := getImage("dog") - if image == nil { - image = getDefaultGoose() - } - - msg := fmt.Sprintf("@%s woof woof woof #honkbot", mention) - sendTweet(twitterAPI, tweet.IdStr, msg, image) + matchTweet(twitterAPI, tweet, "woof", "dog", "woof woof woof") } if commandOinkMatch.MatchString(tweet.Text) { - log.Println("Tweet matched oink") - - if strings.Contains(tweet.Text, "RT ") { - log.Println("This is a RT dont reply to not flood") - return - } - - mention := tweet.InReplyToScreenName - if mention == "" { - mention = tweet.User.ScreenName - } - - image := getImage("pig") - if image == nil { - image = getDefaultGoose() - } - - msg := fmt.Sprintf("@%s oink! oink! #honkbot", mention) - sendTweet(twitterAPI, tweet.IdStr, msg, image) + matchTweet(twitterAPI, tweet, "oink", "pig", "oink! oink!") } if commandQuackMatch.MatchString(tweet.Text) { - log.Println("Tweet matched quack") - - if strings.Contains(tweet.Text, "RT ") { - log.Println("This is a RT dont reply to not flood") - return - } - - mention := tweet.InReplyToScreenName - if mention == "" { - mention = tweet.User.ScreenName - } - - image := getImage("duck") - if image == nil { - image = getDefaultGoose() - } - - msg := fmt.Sprintf("@%s quack! quack! #honkbot", mention) - sendTweet(twitterAPI, tweet.IdStr, msg, image) + matchTweet(twitterAPI, tweet, "quack", "duck", "quack! quack!") } if commandMooMatch.MatchString(tweet.Text) { - log.Println("Tweet matched moo") - - if strings.Contains(tweet.Text, "RT ") { - log.Println("This is a RT dont reply to not flood") - return - } - - mention := tweet.InReplyToScreenName - if mention == "" { - mention = tweet.User.ScreenName - } - - image := getImage("cow") - if image == nil { - image = getDefaultGoose() - } - - msg := fmt.Sprintf("@%s Mooooo! #honkbot", mention) - sendTweet(twitterAPI, tweet.IdStr, msg, image) + matchTweet(twitterAPI, tweet, "moo", "cow", "Mooooo!") } if commandBaaMatch.MatchString(tweet.Text) { - log.Println("Tweet matched baa") + matchTweet(twitterAPI, tweet, "baa", "goat", "baa! baa!!") + } +} - if strings.Contains(tweet.Text, "RT ") { - log.Println("This is a RT dont reply to not flood") - return - } +func matchTweet(twitterAPI *anaconda.TwitterApi, tweet anaconda.Tweet, matched, animal, message string) { + log.Printf("Tweet matched %v", matched) - mention := tweet.InReplyToScreenName - if mention == "" { - mention = tweet.User.ScreenName - } + if strings.Contains(tweet.Text, "RT ") { + log.Println("This is a RT dont reply to not flood") + } - image := getImage("goat") - if image == nil { - image = getDefaultGoose() - } - msg := fmt.Sprintf("@%s baa! baa!! #honkbot", mention) - sendTweet(twitterAPI, tweet.IdStr, msg, image) + mention := tweet.InReplyToScreenName + if mention == "" { + mention = tweet.User.ScreenName + } + + var image []byte + switch animal { + case "pony": + image = getPony() + default: + image = getImage(animal) + } + if image == nil { + image = getDefaultGoose() } + msg := fmt.Sprintf("@%s %v #honkbot", mention, message) + sendTweet(twitterAPI, tweet.IdStr, msg, image) } func sendTweet(twitterAPI *anaconda.TwitterApi, originalTweetID, message string, image []byte) {