Update twitter logic, formatting

This commit is contained in:
Daniel Mason 2021-09-01 21:49:10 +12:00
parent df3c0b9ed4
commit 458b162295
Signed by: idanoo
GPG key ID: 387387CDBC02F132
3 changed files with 19 additions and 53 deletions

View file

@ -63,7 +63,7 @@ func formatCsvTwitterRow(data string) string {
c := make([]string, 0)
c = append(c, fields...)
endtime := strings.Split(c[5], ", ")
return fmt.Sprintf("**%s** - %s _%s_ - _%s_", c[1], c[2], c[4], endtime[1])
return fmt.Sprintf("New Location: *%s\n%s\n_%s_ - _%s_\n#NZCovidBot", c[1], c[2], c[4], endtime[1])
}
func getPostableDiscordData() string {
@ -82,20 +82,3 @@ func getPostableDiscordData() string {
return strings.Join(rows, "\n")
}
func getPostableTwitterData() string {
if len(updatedLocations.Locations) == 0 {
return ""
}
rows := make([]string, 0)
for _, location := range updatedLocations.Locations {
if location.ChangeType == "REMOVED" {
rows = append(rows, fmt.Sprintf("REMOVED: %s", location.TwitterData))
} else {
rows = append(rows, location.TwitterData)
}
}
return strings.Join(rows, "\n\n")
}

View file

@ -34,27 +34,20 @@ func Lesgoooo() {
}
func postTheUpdates() {
// Twitter
go postToTwitter()
// Discord
postableDiscordData := getPostableDiscordData()
if postableDiscordData == "" {
return
}
// Not using go routines so we don't get rate limited
for _, discordWebhook := range DiscordWebhooks {
postToDiscord(discordWebhook, postableDiscordData)
}
// Twitter
// postableTwitterData := getPostableTwitterData()
// if postableTwitterData == "" {
// return
// }
// for _, discordWebhook := range DiscordWebhooks {
// postToTwitter(discordWebhook, postableTwitterData)
// }
// Clear out posted data!
updatedLocations = UpdatedLocations{}
}

View file

@ -16,33 +16,23 @@ type TwitterCredentials struct {
var TwitterCreds TwitterCredentials
// getClient is a helper function that will return a twitter client
// that we can subsequently use to send tweets, or to stream new tweets
// this will take in a pointer to a Credential struct which will contain
// everything needed to authenticate and return a pointer to a twitter Client
// or an error
func getClient(creds *TwitterCredentials) (*twitter.Client, error) {
// Pass in your consumer key (API Key) and your Consumer Secret (API Secret)
config := oauth1.NewConfig(creds.ConsumerKey, creds.ConsumerSecret)
// Pass in your Access Token and your Access Token Secret
token := oauth1.NewToken(creds.AccessToken, creds.AccessTokenSecret)
func postToTwitter() {
if TwitterCreds.AccessTokenSecret == "" {
return
}
config := oauth1.NewConfig(TwitterCreds.ConsumerKey, TwitterCreds.ConsumerSecret)
token := oauth1.NewToken(TwitterCreds.AccessToken, TwitterCreds.AccessTokenSecret)
httpClient := config.Client(oauth1.NoContext, token)
// Twitter client
client := twitter.NewClient(httpClient)
// Verify Credentials
verifyParams := &twitter.AccountVerifyParams{
SkipStatus: twitter.Bool(true),
IncludeEmail: twitter.Bool(true),
// Send a Tweet
for _, row := range updatedLocations.Locations {
_, _, err := client.Statuses.Update(row.TwitterData, nil)
if err != nil {
log.Print(err)
}
}
// we can retrieve the user and verify if the credentials
// we have used successfully allow us to log in!
user, _, err := client.Accounts.VerifyCredentials(verifyParams)
if err != nil {
return nil, err
}
log.Printf("User's ACCOUNT:\n%+v\n", user)
return client, nil
}