├── README.md └── tm.sh /README.md: -------------------------------------------------------------------------------- 1 | [comment]: <> (HEAD) 2 | # TextMagic BASH Wrapper 3 | This library provides you with an easy way of sending SMS and receiving replies by integrating TextMagic SMS Gateway into your PHP application. 4 | 5 | ## What Is TextMagic? 6 | TextMagic’s application programming interface (API) provides the communication link between your application and TextMagic’s SMS Gateway, allowing you to send and receive text messages and to check the delivery status of text messages you’ve already sent. 7 | 8 | For detailed documentation and more examples, please visit [http://docs.textmagictesting.com/](http://docs.textmagictesting.com/). 9 | 10 | [comment]: <> (/HEAD) 11 | 12 | ## Installation Instructions 13 | 1. [Download](https://github.com/textmagic/textmagic-rest-bash) the latest version of the wrapper from GitHub. 14 | 2. Put **tm.sh** into any folder or into PATH, if you want to use it globally. 15 | 16 | ## Requirements 17 | The Shell wrapper for the TextMagic API has the following requirement: 18 | * Bash version 3 or newer. 19 | 20 | ## Send SMS via Shell (an example) 21 | Run this in your command prompt or execute in script: 22 | ```bash 23 | tm.sh send --text="Hello from TextMagic" --phones=447860021130 24 | ``` 25 | [comment]: <> (FOOTER) 26 | ## License 27 | The library is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 28 | 29 | [comment]: <> (/FOOTER) 30 | -------------------------------------------------------------------------------- /tm.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # TextMagic REST API Client 4 | # 5 | # Copyright (C) 2015 TextMagic Ltd. 6 | 7 | readonly E_OK=0 8 | readonly E_INVALID_PARAMETERS=1 9 | readonly E_MISSING_CREDENTIALS=2 10 | 11 | ################ 12 | # CONFIGURATION 13 | ################ 14 | readonly BASE_URL="https://rest.textmagic.com/api/v2" # API base URL 15 | readonly USERNAME="" # Account username 16 | readonly TOKEN="" # API access token 17 | readonly CURLOPTS="" # Custom cURL options 18 | 19 | function usage() { 20 | echo "Usage:" 21 | echo -e "\t$0 send [parameters]" 22 | echo -e "\t$0 [parameters]\n" 23 | echo "Examples:" 24 | echo -e "\t$0 send --phones=99912345,9997890 --text=\"Hello guys\"" 25 | echo -e "\t$0 GET /replies" 26 | echo -e "\t$0 POST /messages --phones=99912345 --text=\"Hello guys\"" 27 | echo -e "\t$0 PUT /user --firstName=Charles --lastName=Conway --company=\"TextMagic Ltd.\"" 28 | echo -e "\t$0 DELETE /contacts/183905" 29 | } 30 | 31 | function request() { 32 | local method path param data curl_string 33 | 34 | if [ -z $BASE_URL ] || [ -z $USERNAME ] || [ -z $TOKEN ]; then 35 | echo "Please configure your access credentials first" 36 | exit $E_MISSING_CREDENTIALS 37 | fi 38 | 39 | method="$1"; [[ $# > 0 ]] && shift 40 | case "$method" in 41 | GET|DELETE|POST|PUT) 42 | if [[ $# = 0 ]] || [ -z "$1" ] || [ "${1#/}" = "$1" ]; then 43 | usage 44 | exit $E_INVALID_PARAMETERS 45 | fi 46 | path=${BASE_URL}${1} && shift 47 | [[ "$method" = "GET" ]] && curlopts="-G" 48 | curlopts="$curlopts $CURLOPTS" 49 | 50 | i=1 51 | for param in "$@"; do 52 | [[ $param == --*=* ]] && param=${param#*--} && data[i]="--data-urlencode '${param%%=*}=${param#*=}'" && i=$((i+1)) 53 | done 54 | 55 | curl_string="curl -s -X $method $curlopts -H 'X-TM-Username: $USERNAME' -H 'X-TM-Key: $TOKEN' ${data[@]} $path" 56 | eval "$curl_string" 57 | 58 | exit $E_OK 59 | ;; 60 | *) 61 | usage 62 | exit $E_INVALID_PARAMETERS 63 | ;; 64 | esac 65 | } 66 | 67 | if [[ "$1" = "send" ]]; then 68 | shift 69 | request POST /messages "$@" 70 | fi 71 | request "$@" --------------------------------------------------------------------------------