From be9962f9f24a3e940ad24f1dc27b75a431bb8ffc Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sun, 7 Aug 2022 16:08:30 +0200 Subject: [PATCH] add tweet command --- cmd/bot/command.go | 7 +++++++ pkg/commands/tweet.go | 20 ++++++++++++++++++++ pkg/decapi/tweet.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 pkg/commands/tweet.go create mode 100644 pkg/decapi/tweet.go diff --git a/cmd/bot/command.go b/cmd/bot/command.go index b77b2dd..29fed9c 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -58,5 +58,12 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) { commands.Echo(target, message.Message[7:len(message.Message)], tc) return } + case "tweet": + if msgLen < 2 { + common.Send(target, "Not enough arguments provided. Usage: ()tweet ", tc) + return + } else { + commands.Tweet(target, cmdParams[1], tc) + } } } diff --git a/pkg/commands/tweet.go b/pkg/commands/tweet.go new file mode 100644 index 0000000..9880af1 --- /dev/null +++ b/pkg/commands/tweet.go @@ -0,0 +1,20 @@ +package commands + +import ( + "github.com/gempir/go-twitch-irc/v3" + "github.com/lyx0/nourybot/pkg/common" + "github.com/lyx0/nourybot/pkg/decapi" + "go.uber.org/zap" +) + +func Tweet(target, username string, tc *twitch.Client) { + sugar := zap.NewExample().Sugar() + defer sugar.Sync() + + resp, err := decapi.Tweet(username) + if err != nil { + sugar.Error(err) + } + + common.Send(target, resp, tc) +} diff --git a/pkg/decapi/tweet.go b/pkg/decapi/tweet.go new file mode 100644 index 0000000..8958745 --- /dev/null +++ b/pkg/decapi/tweet.go @@ -0,0 +1,43 @@ +package decapi + +import ( + "fmt" + "io/ioutil" + "net/http" + + "go.uber.org/zap" +) + +var ( + basePath = "https://decapi.me/twitter/latest/" + twitterUserNotFoundError = "[Error] - [34] Sorry, that page does not exist." +) + +func Tweet(username string) (string, error) { + sugar := zap.NewExample().Sugar() + defer sugar.Sync() + + // https://decapi.me/twitter/latest/forsen?url&no_rts + // ?url adds the url at the end and &no_rts ignores retweets. + resp, err := http.Get(fmt.Sprint(basePath + username + "?url" + "&no_rts")) + if err != nil { + sugar.Error(err) + return "Something went wrong FeelsBadMan", err + } + + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + sugar.Error(err) + return "Something went wrong FeelsBadMan", err + } + + // If the response was a known error message return a message with the error. + if string(body) == twitterUserNotFoundError { + return "Something went wrong: Twitter username not found", err + } else { // No known error was found, probably a tweet. + resp := fmt.Sprintf("Latest Tweet from @%s: \"%s \"", username, body) + return resp, nil + } +}