You need to prepare API keys at first. Go to the front page, create a new app, and generate a new access token.
Then put them as a key file at ~/.tweet.client.key
, with the format:
MY_SCREEN_NAME=xxxxxxxxxxxxxxxxxxx
MY_LANGUAGE=xx
CONSUMER_KEY=xxxxxxxxxxxxxxxxxxx
CONSUMER_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ACCESS_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ACCESS_TOKEN_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
If there is a key file named tweet.client.key
in the current directory, tweet.sh
will load it.
Otherwise, the file ~/.tweet.client.key
will be used as the default key file.
Moreover, you can give those information via environment variables without a key file.
$ export MY_SCREEN_NAME=xxxxxxxxxxxxxxxxxxx
$ export MY_LANGUAGE=xx
$ export CONSUMER_KEY=xxxxxxxxxxxxxxxxxxx
$ export CONSUMER_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$ export ACCESS_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$ export ACCESS_TOKEN_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
$ ./tweet.sh post "Hello!"
This form will be useful to implement a bot program.
And, this script uses some external commands.
You need to install them via package system on your environment: apt
, yum
or something.
Required commands are:
curl
jq
nkf
openssl
$ ./tweet.sh [command] [...arguments]
Available commands are:
help
: shows usage of thetweet.sh
itself.- Reading existing tweets (require "Read" permission)
fetch
(get
,show
): fetches a JSON string of a tweet.search
: searches tweets with queries.fetch-favorites
(fetch-fav
): fetches favorite tweets.fetch-tweets
(fetch-posts
): fetches tweets of a user.watch-mentions
(watch
): watches mentions, retweets, DMs, etc., and executes handlers for each event.type
: detects the type of the given input.body
: extracts the body of a tweet.owner
: extracts the owner of a tweet.showme
: reports the raw information of yourself.whoami
: reports the screen name of yourself.language
(lang
): reports the selected language of yourself.
- Making some changes (require "Write" permission)
post
(tweet
,tw
): posts a new tweet.reply
: replies to an existing tweet.upload
: uploads a media file.delete
(del
,remove
,rm
): deletes a tweet.favorite
(fav
): marks a tweet as a favorite.unfavorite
(unfav
): removes favorited flag of a tweet.retweet
(rt
): retweets a tweet.unretweet
(unrt
): deletes the retweet of a tweet.follow
: follows a user.unfollow
: unfollows a user.
- Operate direct messages (require "Access direct messages" permission)
fetch-direct-messages
(fetch-dm
,get-direct-messages
,get-dm
): fetches recent DMs.direct-message
(dm
): sends a DM.
- Misc.
resolve
: resolves a shortened URL.resolve-all
: resolve all shortened URLs in the given input.
If you want to handle DMs by the watch-mentions
command, you have to permit the app to access direct messages.
Detailed logs can be shown with the DEBUG
flag, like:
$ env DEBUG=1 ./tweet.sh search -q "Bash"
This script is mainly designed to be a client library to implement Twitter bot program, instead for daily human use. For most cases this script reports response JSONs of Twitter's APIs via the standard output. See descriptions of each JSON: a tweet, an event, and other responses also.
Some commands require URL of a tweet, and they accept shortened URLs like http://t.co/***
. Such URLs are automatically resolved as actual URLs like https://twitter.com/***/status/***
. The detectipn pattern for such shortened URLs is defined as URL_REDIRECTORS
in the script, and it must be updated for new services.
-
Parameters
- 1st argument: the ID or the URL of the tweet.
-
Standard output
-
Example
$ ./tweet.sh fetch 0123456789 $ ./tweet.sh fetch https://twitter.com/username/status/0123456789 $ ./tweet.sh get 0123456789 $ ./tweet.sh show 0123456789
-
Parameters
-q
: queries. If you specify no query, then you'll see sample tweets as results.-c
: maximum number of tweets to be responded. 10 by default. (optional)-s
: the id of the last tweet already known. (optional) If you specify this option, only tweets newer than the given tweet will be returned.-m
: the id of the tweet you are searching tweets older than it. (optional) If you specify this option, only tweets older than the given tweet will be returned.-t
: type of results. (optional) Possible values:recent
(default),popular
, ormixed
.-h
: command line to run for each search result. (optional) (It will receive tweets via the standard input.)-w
: start watching without handler. (optional)
-
Standard output
-
Example
$ ./tweet.sh search -q "queries" -c 10 $ ./tweet.sh search -q "Bash OR Shell Script" $ ./tweet.sh search -q "Bash OR Shell Script" -h 'echo "found!"; cat' $ ./tweet.sh search -q "Bash OR Shell Script" -w | while read -r tweet; do echo "found!: ${tweet}"; done
-
Parameters
-u
: the screen name of the owner favorites to be fetched from. Yourself by default.-c
: maximum number of tweets to be fetched. 10 by default.-s
: the id of the last tweet already known. (optional) If you specify this option, only tweets newer than the given tweet will be returned.-m
: the id of the tweet you are searching tweets older than it. (optional) If you specify this option, only tweets older than the given tweet will be returned.
-
Standard output
-
Example
$ ./tweet.sh fetch-favorites -c 20 $ ./tweet.sh fetch-fav -c 10 -s 0123456789
-
Parameters
-u
: the screen name of the owner of tweets to be fetched from. Yourself by default.-c
: maximum number of tweets to be fetched. 10 by default.-s
: the id of the last tweet already known. (optional) If you specify this option, only tweets newer than the given tweet will be returned.-m
: the id of the tweet you are searching tweets older than it. (optional) If you specify this option, only tweets older than the given tweet will be returned.-a
: include replies.-r
: include retweets.
-
Standard output
-
Example
$ ./tweet.sh fetch-tweets -u screen_name -c 20 $ ./tweet.sh fetch-posts -u screen_name -c 10 -s 0123456789
Basically this command provides ability to get search result based on the given query.
If you want to observe new tweets matched to the query continuously, specify a callback command line as the handler via the -h
option.
$ ./tweet.sh search -q "queries" -h "echo 'FOUND'; cat"
In this case, only -q
and -h
options are available.
The script doesn't exit automatically if you specify the -h
option.
To stop the process, you need to send the SIGINT
signal via Ctrl-C or something.
Important note: you cannot use this feature together with watch-mentions
command. Only one streaming API is allowed for you at once.
If you want to watch search results with mentions, use the -k
and -s
options of the watch-mentions
command.
watch-mentions
(watch
): watches mentions, retweets, DMs, etc., and executes handlers for each event.
-
Parameters
-k
: comma-separated list of tracking keywords.-m
: command line to run for each reply or mention. (optional) (It will receive mention tweets via the standard input.)-r
: command line to run for each retweet. (optional) (It will receive retweet tweets via the standard input.)-q
: command line to run for each quotation. (optional) (It will receive quotation tweets via the standard input.)-f
: command line to run when a user follows you. (optional) (It will receivefollow
event via the standard input.)-d
: command line to run when a DM is received. (optional) (It will receive quotation tweets via the standard input.)-s
: command line to run for each search result, matched to the keywords given via the-k
option. (optional)
-
Standard output
- Nothing.
-
Example 1: without handlers
$ ./tweet.sh watch-mentions -k "keyword1,keyword2,..." | while read -r event; do echo "event: ${event}"; done
-
Example 2: with handlers
$ ./tweet.sh watch-mentions -k "keyword1,keyword2,..." \ -r "echo 'REPLY'; cat" \ -t "echo 'RT'; cat" \ -q "echo 'QT'; cat" \ -f "echo 'FOLLOWED'; cat" \ -d "echo 'DM'; cat" \ -s "echo 'SEARCH-RESULT'; cat"
This command provides ability to observe various events around you or any keyword.
In this case this script stays running.
To stop the process, you need to send the SIGINT
signal via Ctrl-C or something.
Important note: you cannot use this feature together with search
command with a handler. Only one streaming API is allowed for you at once.
If you want to watch search results with mentions, use the -k
and -s
options instead of the search
command.
-
Parameters
- The standard input: a JSON string of a tweet.
-k
: comma-separated list of keywords which are used for "search".
-
Standard output
- The data type detected from the input.
Possible values:
event-follow
: An event when you are followed.direct-message
: A direct message. It can be wrapped with a keydirect_message
.quotation
: A commented RT.retweet
: An RT.mention
: A mention or reply.search-result
: A tweet which is matched to the given keywords.
- The data type detected from the input.
Possible values:
-
Example
$ echo "$tweet_json" | ./tweet.sh type -k keyword1,keyword2
This command provides ability to detect the type of each object returned from the user stream.
For unknown type input, this returns an exit status 1
and reports nothing.
-
Parameters
- The standard input: a JSON string of a tweet. (optional)
- 1st argument: the ID or the URL of a tweet. (optional)
-
Standard output
- The body string of the tweet.
-
Example
$ ./tweet.sh body 0123456789 $ ./tweet.sh body https://twitter.com/username/status/0123456789 $ echo "$tweet_json" | ./tweet.sh body
-
Parameters
- The standard input: a JSON string of a tweet. (optional)
- 1st argument: the ID or the URL of a tweet. (optional)
-
Standard output
- The screen name of the owner.
-
Example
$ ./tweet.sh owner 0123456789 $ ./tweet.sh owner https://twitter.com/username/status/0123456789 $ echo "$tweet_json" | ./tweet.sh owner
-
Parameters
- Nothing.
-
Standard output
-
Example
$ ./tweet.sh showme
This will be useful if you want to get both informations whoami
and language
at once.
-
Parameters
- Nothing.
-
Standard output
- The screen name of yourself.
-
Example
$ ./tweet.sh whoami username
Important note: the rate limit of the API used by this command is very low. If you want to call another language
command together, then you should use showme
command instead.
-
Parameters
- Nothing.
-
Standard output
- The language code selected by yourself.
-
Example
$ ./tweet.sh language en $ ./tweet.sh lang en
Important note: the rate limit of the API used by this command is very low. If you want to call another whoami
command together, then you should use showme
command instead.
-
Parameters
-m
: comma-separated list of uploaded media IDs. See also theupload
command.-l
: add location to tweet. (optional)- All rest arguments: the body of a new tweet to be posted. If you don't specify no extra parameters, this command reads posting body from the standard input.
-
Standard output
-
Example
$ ./tweet.sh post A tweet from command line $ ./tweet.sh post 何らかのつぶやき $ ./tweet.sh tweet @friend Good morning. $ ./tweet.sh tw -m 123,456,789 My Photos! $ ./tweet.sh post -l A tweet with location $ cat body.txt | ./tweet.sh post
All rest arguments following to the command name are posted as a tweet. If you include a user's screen name manually in the body, it will become a mention (not a reply).
-
Parameters
-m
: comma-separated list of uploaded media IDs. See also theupload
command.- 1st rest argument: the ID or the URL of a tweet to be replied.
- All other rest arguments: the body of a new reply to be posted. If you don't specify no extra parameters, this command reads posting body from the standard input.
-
Standard output
-
Example
$ ./tweet.sh reply 0123456789 @friend A regular reply $ ./tweet.sh reply 0123456789 A silent reply $ ./tweet.sh reply https://twitter.com/username/status/0123456789 @friend A regular reply $ ./tweet.sh reply https://twitter.com/username/status/0123456789 A silent reply $ ./tweet.sh reply 0123456789 -m 123,456,789 Photo reply $ cat body.txt | ./tweet.sh reply 0123456789
Note that you have to include the user's screen name manually if it is needed. This command does not append it automatically.
-
Parameters
- 1st argument: absolute path to a local file.
-
Standard output
-
Example
$ ./tweet.sh upload /path/to/file.png
-
Parameters
- 1st argument: the ID or the URL of a tweet to be deleted.
-
Standard output
-
Example
$ ./tweet.sh delete 0123456789 $ ./tweet.sh del https://twitter.com/username/status/0123456789 $ ./tweet.sh remove 0123456789 $ ./tweet.sh rm https://twitter.com/username/status/0123456789
-
Parameters
- 1st argument: the ID or the URL of a tweet to be favorited.
-
Standard output
-
Example
$ ./tweet.sh favorite 0123456789 $ ./tweet.sh favorite https://twitter.com/username/status/0123456789 $ ./tweet.sh fav 0123456789 $ ./tweet.sh fav https://twitter.com/username/status/0123456789
-
Parameters
- 1st argument: the ID or the URL of a tweet to be unfavorited.
-
Standard output
-
Example
$ ./tweet.sh unfavorite 0123456789 $ ./tweet.sh unfavorite https://twitter.com/username/status/0123456789 $ ./tweet.sh unfav 0123456789 $ ./tweet.sh unfav https://twitter.com/username/status/0123456789
-
Parameters
- 1st argument: the ID or the URL of a tweet to be retweeted.
-
Standard output
-
Example
$ ./tweet.sh retweet 0123456789 $ ./tweet.sh retweet https://twitter.com/username/status/0123456789 $ ./tweet.sh rt 0123456789 $ ./tweet.sh rt https://twitter.com/username/status/0123456789
Note, you cannot add extra comment for the retweet.
Instead, if you want to "quote" the tweet, then you just have to post
with the URL of the original tweet.
$ ./tweet.sh post Good news! https://twitter.com/username/status/0123456789
-
Parameters
- 1st argument: the ID or the URL of a tweet to be unretweeted.
-
Standard output
-
Example
$ ./tweet.sh unretweet 0123456789 $ ./tweet.sh unretweet https://twitter.com/username/status/0123456789 $ ./tweet.sh unrt 0123456789 $ ./tweet.sh unrt https://twitter.com/username/status/0123456789
-
Parameters
- 1st argument: the screen name of a user to be followed, or a URL of a tweet.
-
Standard output
-
Example
$ ./tweet.sh follow @username $ ./tweet.sh follow username $ ./tweet.sh follow https://twitter.com/username/status/012345
-
Parameters
- 1st argument: the screen name of a user to be unfollowed, or a URL of a tweet.
-
Standard output
-
Example
$ ./tweet.sh unfollow @username $ ./tweet.sh unfollow username $ ./tweet.sh unfollow https://twitter.com/username/status/012345
#Operate direct messages
-
Parameters
-c
: maximum number of messages to be fetched. 10 by default.
-
Standard output
-
Example
$ ./tweet.sh fetch-direct-messages -c 20 $ ./tweet.sh get-direct-messages -c 20
-
Parameters
- All arguments: the body of a new direct message to be sent. If you don't specify no parameter, this command reads message body from the standard input.
-
Standard output
-
Example
$ ./tweet.sh direct-message @friend Good morning. $ ./tweet.sh direct-message friend Good morning. $ ./tweet.sh dm @friend Good morning. $ ./tweet.sh dm friend Good morning. $ cat body.txt | ./tweet.sh direct-message @friend
#Misc.
-
Parameters
- 1st argument: a shortened URL.
-
Standard output
- The resolved original URL.
-
Example
$ ./tweet.sh resolve https://t.co/xxxx
-
Parameters
- Nothing.
-
Standard output
- The given input with resolved URLs.
-
Example
$ cat ./tweet-body.txt | ./tweet.sh resolve-all