Пример работы с SMS шлюзом для командной обочки Bash

Скрипт для оболочки Bash позволяет отправлять сообщения прямо с консоли пользователя, протестирован на версии 4.4.12.

Отправка смс
Получения статуса отправленной смс
Получение цены отправки
Получение баланса


Отправка смс:

#!/bin/bash
set -u

LOGIN='your login'
PASSWORD='your password'
URL='http://api.myatompark.com/members/sms/xml.php'

function sendSms {
    local phones
    phones=("${@:5}")

    local phonesXml
    for ix in ${!phones[*]}
    do
       phonesXml+="${phones[$ix]}"
    done
    local data="XML=\
        \
            \
                SEND\
            \
            \
                $1\
                $2\
            \
            \
                $3\
                $4\
            \
            ${phonesXml}\
        "
    #echo -e "$data"
    curl --data "$data" $URL
}

function getPrice {
    local phones
    phones=("${@:5}")

    local phonesXml
    for ix in ${!phones[*]}
    do
       phonesXml+="${phones[$ix]}"
    done
    local data="XML=\
        \
            \
                GETPRICE\
            \
            \
                $1\
                $2\
            \
            \
                $3\
                $4\
            \
            ${phonesXml}\
        "
    #echo -e "$data" 
    curl --data "$data" $URL
}

function getStatus {
    local numbers
    numbers=("${@:3}")
    local statisticsXml
    for ix in ${!numbers[*]}
    do
       statisticsXml+="${numbers[$ix]}"
    done
    local data="XML=\
        \
            \
                GETSTATUS\
            \
            \
                $1\
                $2\
            \
            \
                $statisticsXml\
            \
        "
    #echo -e "$data" 
    curl --data "$data" $URL
}

function getBalance {
    local data="XML=\
        \
            \
                BALANCE\
            \
            \
                $1\
                $2\
            \
        "
    #echo -e "$data" 
    curl --data "$data" $URL
}

function getCreditPrice {
    local data="XML=\
        \
            \
                CREDITPRICE\
            \
            \
                $1\
                $2\
            \
        "
    #echo -e "$data" 
    curl --data "$data" $URL
}

function logging {
    echo -en "$1";
}

args=("$@")

usage="$(basename "$0") [-h] [-a (send|price|status|credit_price|balance)] [-s n] [-t n] [-p n] [-n n] -- xml sms gateway for sending sms

where:
    -h  show this help text
    -a  \033[4m action_name \033[0m
        \033[1m send \033[0m send sms on phones
        \033[1m price \033[0m price sms on phones
        -s  sender name
        -t  text sms
        -p  phones (example: '380933630000 380933630001')
        \033[1m status \033[0m show status on phones by phone's id (special attributes)
        -n  phones's id
        \033[1m balance \033[0m show user banance
        \033[1m credit_price \033[0m show user price"

while getopts ":hs:a:s:t:p:" opt; do
    case $opt in
        a) ACTION="${OPTARG}" ;;
        s) SENDER="${OPTARG}" ;;
        t) TEXT="${OPTARG}" ;;
        n) NUMBERS="${OPTARG}" ;;
        p) STR_PHONES="${OPTARG}" ;;
        h) echo -en "$usage"
           printf "\n"
           exit
           ;;
        \?) printf "illegal option: -%s\n" "$OPTARG" >&2
            echo -en "$usage" >&2
            printf "\n"
            exit 1
            ;;
        :) logging "Option -${OPTARG} requires an argument." ; exit 252 ;;
    esac
done
shift $(($OPTIND - 1))


if [[ "${ACTION:-unset}" == "unset" ]]; then
    echo -e "Parameters ACTION not found. "
    exit 1
fi

case $ACTION in
    send)
        if [[ "${SENDER:-unset}" == "unset" ]]; then
            echo -e "Parameters SENDER not found. "
            exit 1
        fi
        if [[ "${TEXT:-unset}" == "unset" ]]; then
            echo -e "Parameters TEXT not found. "
            exit 1
        fi
        if [[ "${STR_PHONES:-unset}" == "unset" ]]; then
            echo -e "Parameters PHONES not found. "
            exit 1
        fi
        PHONES=()
        for phone in $STR_PHONES; do
            PHONES+=("$phone")
        done
        sendSms $LOGIN $PASSWORD "$SENDER" "$TEXT" ${PHONES[*]} ;;
    price)
        if [[ "${SENDER:-unset}" == "unset" ]]; then
            echo -e "Parameters SENDER not found. "
            exit 1
        fi
        if [[ "${TEXT:-unset}" == "unset" ]]; then
            echo -e "Parameters TEXT not found. "
            exit 1
        fi
        if [[ "${STR_PHONES:-unset}" == "unset" ]]; then
            echo -e "Parameters PHONES not found. "
            exit 1
        fi
        PHONES=()
        for phone in $STR_PHONES; do
            PHONES+=("$phone")
        done
        getPrice $LOGIN $PASSWORD "$SENDER" "$TEXT" ${PHONES[*]} ;;
    status)
        if [[ "${NUMBERS:-unset}" == "unset" ]]; then
            echo -e "Parameters NUMBERS not found. "
            exit 1
        fi
        PHONES=()
        for phone in $NUMBERS; do
            PHONES+=("$phone")
        done
        getStatus $LOGIN $PASSWORD ${PHONES[*]} ;;
    credit_price)
        getCreditPrice $LOGIN $PASSWORD ;;
    balance)
        getBalance $LOGIN $PASSWORD ;;
    :) logging "Option -${OPTARG} requires an argument." ; exit 252 ;;
esac
printf "\n"

Есть вопрос?

Дарья


0 (800) 21 01 91;
+38 (093) 672-52-57
+38 (095) 446-56-21
skype:dkepochta
support@epochta.com.ua