There is a small script, sample.py
, which demonstrates how to import parts of the library and use them,
run it from the terminal with:
python sample.py
All examples assume you have a mobily.ws account with an available balance
Managing your mobily.ws account
Request to change account password:
from mobily.utilities import MobilyApiAuth
from mobily.account import MobilyAccount
account = MobilyAccount(MobilyApiAuth('966555555555', 'demo'))
account.change_password('TrustNo1')
Get current password sent to email or phone:
account.forgot_password() # send to email registered on account
account.forgot_password(send_to_email=False) # send to phone registered on account
Check the available balance on the account:
balance = account.check_balance()
print '{0} credits available, total {1}'.format(balance['current'], balance['total'])
Configuring Senders
Check the activation status of all previously requested senders:
from mobily.utilities import MobilyApiAuth
from mobily.sender import MobilySender
sender = MobilySender(MobilyApiAuth('966555555555', 'demo'))
senders_by_status = sender.get_activation_status_for_all_senders()
print 'Active Senders:', [alpha_sender for alpha_sender in senders_by_status['active']]
print 'Pending Senders:', [alpha_sender for alpha_sender in senders_by_status['pending']]
print 'Inactive Senders:', [alpha_sender for alpha_sender in senders_by_status['notActive']]
Request to add a new sender word name:
sender.request_alphabetical_license('NEW SMS')
Two step process for activating a mobile number as a sender name:
sender_id = sender.request_mobile_number_license('966444444444')
# the above call returns an id if successful, and a code is sent via SMS to the number
sender.activate_mobile_number_license(sender_id, 'CODE_FROM_SMS')
# check it worked
if sender.is_mobile_number_license_active(sender_id):
print 'Activated!'
Sending SMS messages
Check the Mobily.ws SMS sending service is available:
from mobily.sms import MobilySMS
if MobilySMS.can_send():
print 'Service is available!'
Send SMS, immediately, saying 'Hello, World' to 966444444444, from 'PYTHON':
from mobily.utilities import MobilyApiAuth
from mobily.sms import MobilySMS
sms = MobilySMS(MobilyApiAuth('966555555555', 'demo'))
sms.add_number('966444444444')
sms.sender = 'PYTHON'
sms.msg = 'Hello, World!'
sms.send()
As above, but using constructor, and sending to multiple numbers:
auth = MobilyApiAuth('966555555555', 'demo')
sms = MobilySMS(auth, ['96202258669', '967965811686'], 'PYTHON', 'Hello, World!')
sms.send()
As above, but schedule to send on 25th December 2020 at midday:
auth = MobilyApiAuth('966555555555', 'demo')
sms = MobilySMS(auth, ['96202258669', '967965811686'], 'PYTHON', 'Hello, World!')
sms.schedule_to_send_on(25, 12, 2020, 12, 0, 0)
sms.delete_key = '666'
sms.send()
Delete the above scheduled SMS before it sends:
sms.delete()
Send a bulk SMS to multiple people, letting them know about their subscription, with personalised messages just for them:
from mobily.utilities import MobilyApiAuth
from mobily.sms import MobilyFormattedSMS
auth = MobilyApiAuth('966555555555', 'demo')
msg = 'Hi (1), your subscription will end on (2).'
sms = MobilyFormattedSMS(auth, ['966505555555', '966504444444'], 'NEW SMS', msg)
sms.add_variable_for_number('966505555555', '(1)', 'Ahmad')
sms.add_variable_for_number('966505555555', '(2)', '31/12/2013')
sms.add_variable_for_number('966504444444', '(1)', 'Mohamed')
sms.add_variable_for_number('966504444444', '(2)', '01/11/2013')
sms.send()
Handling errors
When a request has been unsuccessful, whether due to a known error (insufficient balance), or otherwise, an MobilyApiError is raised.
This error contains a message in English and Arabic.
from mobily.utilities import MobilyApiAuth, MobilyApiError
from mobily.account import MobilyAccount
account = MobilyAccount(MobilyApiAuth('DOESNT_EXIST', 'demo'))
try:
response = account.check_balance()
except MobilyApiError as error:
print error.msg_english, error.msg_arabic
Tests for the core logic behind the utilities can be run from the terminal with:
python -m unittest discover -v