Tweet regularly with the Go language Twitter API

What to prepare

・ MAC (PC) ・ AWS Lambda ・ Twitter API ・ Twitter account

What i did

1. Twitter API application

Apply for Twitter API from the URL below. https://developer.twitter.com/ja/docs

Get the API key & secret and Access token & secret.

2. Write in Go language

Write the source code for Twitter authentication. You don't have to separate the files separately.

keys.go


package keys

import 
(
    //This is the import required for the Twitter API. If not, install it.
    "github.com/ChimeraCoder/anaconda"
)

func GetTwitterApi() *anaconda.TwitterApi {
	anaconda.SetConsumerKey("API Key")
	anaconda.SetConsumerSecret("API secret Key")
	api := anaconda.NewTwitterApi("token", "token secret")
	return api
}

Write the source code of the tweet body. This can also be done without separating the files. I try to tweet the date and text.

text.go


package text

import (
	"fmt"
	"time"
)

func TextTweet() string {
    //I'll write a tweet
    tweetText := "test"

    d := time.Now().Day()
    m := time.Now().Month()

    //Store the text to tweet.
    TweetContent := tweetText
    TweetOfToday := fmt.Sprintf("【%d month%d day]\n %s", m, d, TweetContent)
    return TweetOfToday
}

Write the source to actually tweet.

main.go


package main

import (
	"fmt"
	. "fmt"

	. "./keys"
	. "./text"
)
func main(){
    api := GetTwitterApi()
    text := TextTweet()
	
    tweet, err := api.PostTweet(text, nil)
    if err != nil {
       panic(err)
    }
     Print(tweet.Text)

     fmt.Println("Finish!")
}

By the way, the folder structure is as follows.

autoTweet
  ∟main.go
 ∟keys
   ∟keys.go
  ∟text
   ∟text.go

3. Run on AWS Lambda

Upload the Go source code to AWS Lambda as a zip file. ↓ was very helpful. https://dev.classmethod.jp/articles/aws-lambda-supports-go/

You need a binary file to upload and run to AWS Lambda. I put them together in one file (main.go) into a binary file. The binary file creation command is ↓. Only binary files can be zipped.

#GOOS=linux GOARCH=amd64 go build main.go

If you get an error creating the binary file, try the following:

#go get -u golang.org/x/sys/unix
#set GOOS=linux
#set GOARCH=amd64
#set CGO_ENABLED=0

If you test it with Lambda and you don't get an execution error, go to Twitter! It should have been tweeted!

4. Run Lambda regularly in CloudWatch Events.

Run Lambda periodically with the Cron expression of CloudWatch Events. The Cron expression can be executed once every two months or at any time depending on the setting. Reference URL: https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/services-cloudwatchevents-expressions.html

For reference, I will describe the cron expression that is executed at 12:00 every day. (Because of UTC, it will be -9 hours)

crons expression:
00 3 * * ? *

5. Tweet confirmation

It's OK if you tweet at the set time!

I wrote it quite roughly, but I hope it helps someone. .. ..

Recommended Posts

Tweet regularly with the Go language Twitter API
Specifying the date with the Twitter API
Tweet using the Twitter API in Python
[Go language] Use OpenWeatherMap and Twitter API to regularly tweet weather information from Raspberry Pi
Hit the Twitter API after Oauth authentication with Django
Crawl the URL contained in the twitter tweet with python
Create a tweet heatmap with the Google Maps API
Use Twitter API with Python
Try using the Twitter API
Try using the Twitter API
Support yourself with Twitter API
Call the API with python3.
Successful update_with_media with twitter API
Try hitting the Twitter API quickly and easily with Python
Streamline information gathering with the Twitter API and Slack bots
[Go language] Collect and save Vtuber images using Twitter API
Tweet Now Playing to Twitter using the Spotify API. [Python]
Let's touch on the Go language
Hit the Etherpad-lite API with Python
I liked the tweet with python. ..
Using cgo with the go command
Access the Twitter API in Python
[Memo] Tweet on twitter with python
I made a Twitter Bot with Go x Qiita API x Lambda
Tweet the weather forecast with a bot
Collecting information from Twitter with Python (Twitter API)
Access the Docker Remote API with Requests
Touch around the twitter list with tweepy
I tried to delete bad tweets regularly with AWS Lambda + Twitter API
Tweet from python with Twitter Developer + Tweepy
Try using Dropbox API v2 with Go
Post from another account with Twitter API
List of language codes used in twitter (including API) (with Python dictionary). What is the most commonly used language?
Be careful when retrieving tweets at regular intervals with the Twitter API
Get holidays with the Google Calendar API
Extract sudden buzzwords with twitter streaming API
Play with puns using the COTOHA API
Use twitter API (API account registration and tweet acquisition)
I tried hitting the Qiita API from go
Tweet the weather forecast with a bot Part 2
Hello world with full features of Go language
Until you use the Kaggle API with Colab
I wrote the hexagonal architecture in go language
Let's enjoy natural language processing with COTOHA API
Python with Go
It's too easy to access the Twitter API with rauth and I have her ...
Second half of the first day of studying Python Try hitting the Twitter API with Bottle
Play with the power usage API provided by Yahoo
[Linux] Execute git pull regularly with the crontab command
[Boto3] Search for Cognito users with the List Users API
[Life hack] Women's Anna support bot with Twitter API
Ruby expert learned the basic grammar of Go language
Predict the gender of Twitter users with machine learning
Crawling with Python and Twitter API 1-Simple search function
Post to your account using the API on Twitter
Tweet the triple forecast of the boat race on Twitter
Get comments and subscribers with the YouTube Data API
Note calling the CUDA Driver API with Python ctypes
Pick up only crispy Japanese with Twitter streaming API
I moved the automatic summarization API "summpy" with python3.
I tried hitting the API with echonest's python client