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

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

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

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

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

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

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

Отправка СМС:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash
set -u
 
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"
vector
image

Нужна помощь?

Задайте вопрос специалисту технической поддержки