Skip to content

Instantly share code, notes, and snippets.

@andypiper
Forked from shapeshed/twit_twurl.sh
Last active December 16, 2019 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andypiper/8ca73e964c90e72c2e023a84aee866c0 to your computer and use it in GitHub Desktop.
Save andypiper/8ca73e964c90e72c2e023a84aee866c0 to your computer and use it in GitHub Desktop.
Tweet from the command line using twurl & OAuth
################################
#!/usr/bin/env bash
# File: twit_twurl.sh
# Description: Tweet from the command line using twurl & OAuth
#
# Copyright 2010 George Ornbo (Shape Shed)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This script requires that you have installed and setup twurl
# For instructions see: http://github.com/twitter/twurl
#
################################
################################
# Options
################################
# Turn on notifications
# Set to 1 to enable
NOTIFY=0
################################
# API URL Constant
# Handled by twurl but listed here for clarity
################################
TWITTER_API_URL="http://api.twitter.com/1.1/"
################################
# Twurl command
################################
TWURL='twurl'
################################
# Error messages
################################
ERROR_NO_INPUT="I didn't get a Tweet so I can't post it :-( Exiting.."
ERROR_NO_METHOD="No method was supplied for the post request"
ERROR_TOO_LONG="The Tweet is over 280 characters and too long"
ERROR_WRONG_NO_ARGUMENTS="The wrong number of arguments were received for this function"
################################
# Status Constants
################################
STATUS_UPDATE="/statuses/update"
function post_request
{
# We are expecting two arguments
# $1 is the API method, $2 is the string (Tweet)
if [ $# -ne 2 ]
then
echo $ERROR_WRONG_NO_ARGUMENTS
exit 1
fi
# Check we have a method
# TO DO: Put methods in an array and check it is in array
if [ -z "$1" ]
then
echo $ERROR_NO_METHOD
exit 1
fi
# Check we have a Tweet
if [ -z "$2" ]
then
echo $ERROR_NO_INPUT
exit 1
fi
# Check the Tweet is not too long
if [ $(echo "$2" | wc -c) -gt 280 ]; then
echo $ERROR_TOO_LONG
exit 1
fi
$TWURL $1 -d "status=""$2"
if [ $NOTIFY == 1 ]
then
osascript -e 'display notification "Tweet Posted!"'
fi
}
################################
# Read user input
################################
printf "What are you doing today?\n"
read tweet
################################
# Send the Tweet to Twitter!
################################
post_request $STATUS_UPDATE "$tweet"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment