add tweet command

This commit is contained in:
lyx0 2022-08-07 16:08:30 +02:00
parent 802dbd2caf
commit be9962f9f2
3 changed files with 70 additions and 0 deletions

View file

@ -58,5 +58,12 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) {
commands.Echo(target, message.Message[7:len(message.Message)], tc) commands.Echo(target, message.Message[7:len(message.Message)], tc)
return return
} }
case "tweet":
if msgLen < 2 {
common.Send(target, "Not enough arguments provided. Usage: ()tweet <username>", tc)
return
} else {
commands.Tweet(target, cmdParams[1], tc)
}
} }
} }

20
pkg/commands/tweet.go Normal file
View file

@ -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)
}

43
pkg/decapi/tweet.go Normal file
View file

@ -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
}
}