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

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

Скачать скрипт для версии 2.0

Скачать скрипт для версии 3.0

Базовые положения

Для активации смс шлюза необходимо в панели управления сервисом ePochta SMS в «Настройках» активировать использование API. Для версии 2.0 в закладке «ePochta SMS», в пункте «XML интерфейс включен» необходимо выбрать пункт «Да». В пункте «XML интерфейс: режим работы» выбрать либо «Реальная отправка», либо «Тестовый режим». Соответственно для версии 3.0 в пункте «Активировать API 3.0» установите «Да».

Загрузка и установка

Скачайте файл и сохраните в любой папке на вашем компьютере. Обратите внимание, перед использованием срипта вам необходимо заполнить значение параметров LOGIN и PASSWORD для версии 2.0, а также указать privateKey и publicKey для 3.0, которые можно взять в личном кабинете в разделе Настройки смс сервиса

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


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

#!/bin/bash
set -u

URL='http://api.atompark.com/api/sms/3.0/'
VERSION="3.0"
privateKey='your private key'
publicKey='your publicKey key'

function logging {
    echo "$1";
}

function calcControlSum(){
    local action=$1
    local sum=''
    {
        # eval string into a new associative array
        eval "declare -A params="${2#*=}
        # proof that array was successfully created
        declare -p params
    } &> /dev/null
    params[version]="$VERSION"
    params[action]="$action"
    local keys=()
    for i in "${!params[@]}"
    do
        keys+=($i)
    done
    local skeys=($(echo "${keys[@]}" | sed 's/ /\n/g' | sort))
    for i in "${!skeys[@]}"
    do
        sum+=${params[${skeys[$i]}]}
    done
    sum+="$privateKey"
    echo -n "$sum" | md5sum | cut -d' ' -f1
}
args=("$@")

function sendSms {
    local action="sendSMS"
    local sender=$1
    local text=$2
    local phone=$3
    local datetime=$4
    local sms_lifetime=$5
    {
        declare -A params
        params=(
            [sender]="$sender"
            [text]="$text"
            [phone]="$phone"
            [datetime]="$datetime" 
            [sms_lifetime]="$sms_lifetime"
            [key]="$publicKey"
        )
        sum=$(calcControlSum "$action" "$(declare -p params)")
    } &> /dev/null
    curl --request POST --data "sender=$sender&text=$text&phone=$phone&datetime=$datetime&sms_lifetime=$sms_lifetime&key=$publicKey&sum=$sum" "$URL$action"
}

function addAddressbook {
    local action="addAddressbook"
    local name=$1
    local description=$2
    {
        declare -A params
        params=(
            [name]="$name"
            [description]="$description"
            [key]="$publicKey"
        )
        sum=$(calcControlSum "$action" "$(declare -p params)")
    } &> /dev/null
    curl --request POST --data "name=$name&description=$description&key=$publicKey&sum=$sum" "$URL$action"
}

function addPhoneToAddressBook {
    local action="addPhoneToAddressBook"
    local idAddressBook=$1
    local phone=$2
    local variables=$3
    {
        declare -A params
        params=(
            [idAddressBook]="$idAddressBook"
            [phone]="$phone"
            [variables]="$variables"
            [key]="$publicKey"
        )
        sum=$(calcControlSum "$action" "$(declare -p params)")
    } &> /dev/null
    curl --request POST --data "idAddressBook=$idAddressBook&phone=$phone&variables=$variables&key=$publicKey&sum=$sum" "$URL$action"
}

function getUserBalance {
    local action="getUserBalance"
    local currency=$1
    {
        declare -A params
        params=(
            [currency]="$currency"
            [key]="$publicKey"
        )
        sum=$(calcControlSum "$action" "$(declare -p params)")
    } &> /dev/null
    curl --request POST --data "currency=$currency&key=$publicKey&sum=$sum" "$URL$action"
}

function createCampaign {
    local action="createCampaign"
    local sender=$1
    local text=$2
    local list_id=$3
    local datetime=$4
    local batch=$5
    local batchinterval=$6
    local sms_lifetime=$7
    local control_phone=$8
    {
        declare -A params
        params=(
            [sender]="$sender"
            [text]="$text"
            [list_id]="$list_id"
            [datetime]="$datetime"
            [batch]="$batch"
            [batchinterval]="$batchinterval"
            [sms_lifetime]="$sms_lifetime"
            [control_phone]="$control_phone"
            [key]="$publicKey"
        )
        sum=$(calcControlSum "$action" "$(declare -p params)")
    } &> /dev/null
    curl --request POST --data "sender=$sender&text=$text&list_id=$list_id&datetime=$datetime&batch=$batch&batchinterval=$batchinterval&sms_lifetime=$sms_lifetime&control_phone=$control_phone&key=$publicKey&sum=$sum" "$URL$action"
}

usage="$(basename "$0") [-h] [-a (send|balance|add_addressbook|add_phone|create_campaign)] [-s n] [-t n] [-p n] [-n n] -- api v3.0 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
            -s  sender name
            -t  text sms
            -p  phones (example: '380933630000')
            -d  datetime (default: '')
            -l  lifetime (default: 0)
            -n  id's of phones
        \033[1m add_addressbook \033[0m send sms on phones
            -b  address book name
            -d  description of book
        \033[1m add_phone \033[0m send sms on phones
            -i  id of address book 
            -p  phones (example: '380933630000')
        \033[1m create_campaign \033[0m send sms on phones
            -s  sender name
            -t  text sms
            -i  id of address book 
            -d  datetime (default: '')
            -l  lifetime (default: 0)
        \033[1m balance \033[0m user's balance
            -c  currency (default: USD)"

while getopts ":hs:i:l:a:b:s:t:p:n:" opt; do
    case $opt in
        a) ACTION="${OPTARG}" ;;
        b) BOOK_NAME="${OPTARG}" ;;
        c) CURRENCY="${OPTARG}" ;;
        i) BOOK_ID="${OPTARG}" ;;
        d) DATETIME="${OPTARG}" ;;
        l) LIFETIME="${OPTARG}" ;;
        s) SENDER="${OPTARG}" ;;
        t) TEXT="${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 STR_PHONES not found. "
            exit 1
        fi
        if [[ "${DATETIME:-unset}" == "unset" ]]; then
            DATETIME=""
        fi
        if [[ "${LIFETIME:-unset}" == "unset" ]]; then
            LIFETIME="0"
        fi
        sendSms "$SENDER" "$TEXT" "$STR_PHONES" "$DATETIME" "$LIFETIME" ;;
    balance)
        if [[ "${CURRENCY:-unset}" == "unset" ]]; then
            CURRENCY="USD"
        fi
        getUserBalance "$CURRENCY" ;;
    add_addressbook)
        if [[ "${BOOK_NAME:-unset}" == "unset" ]]; then
            echo -e "Parameters BOOK_NAME not found. "
            exit 1
        fi
        addAddressbook "$BOOK_NAME" "" ;;
    add_phone)
        if [[ "${BOOK_ID:-unset}" == "unset" ]]; then
            echo -e "Parameters BOOK_ID not found. "
            exit 1
        fi
        if [[ "${STR_PHONES:-unset}" == "unset" ]]; then
            echo -e "Parameters STR_PHONES not found. "
            exit 1
        fi
        addPhoneToAddressBook "$BOOK_ID" "$STR_PHONES" "" ;;
    create_campaign)
        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 [[ "${BOOK_ID:-unset}" == "unset" ]]; then
            echo -e "Parameters BOOK_ID not found. "
            exit 1
        fi
        if [[ "${DATETIME:-unset}" == "unset" ]]; then
            DATETIME=""
        fi
        if [[ "${LIFETIME:-unset}" == "unset" ]]; then
            LIFETIME="0"
        fi
        createCampaign "$SENDER" "$TEXT" "$BOOK_ID" "$DATETIME" "$LIFETIME" 0 0 "" ;;
    :) 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