v1.30. The sms service creates a service that can send mobile messages via
APIs. Multiple SMS APIs are supported:
Here is a sample config for Amazon SNS:
sms:
amazonsns:
type: amazonsns
aws_access_key_id: ...
aws_secret_access_key: ...
region_name: ap-southeast-1
smstype: Transactional
To set up SNS:
pip install boto3AmazonSNSFullAccess policy enabledgramex.yamlThe smstype parameter for Amazon SNS can be either
Transactional: for operational messagesPromotional: for sales / marketing messages. These may not be delivered
to numbers in the DNC registry, and may be rate-limited.Notes:
Here is a sample config for Exotel:
sms:
exotel:
type: exotel
sid: ... # Account Sid
key: ... # API KEY
token: ... # API TOKEN
domain: ... # Optional. Defaults to api.exotel.com
priority: high # Optional. Can be high or normal. Defaults to high
To set up Exotel:
OTP requested by %s with mobile %s.. Request approval.gramex.yaml.sid: Account Sidkey: API KEYtoken: API TOKENNotes:
+91 9741 552 552, 09741552552, 9741-552-552 are the same.No SMS SenderId found for Account=[...], From=[...]%s for numbers as well as text, despite the Exotel documentationmobile %s is invalid. But mobile %s. with a period (.) at the end is valid.See the Exotel documentation at https://developer.exotel.com/api/#send-sms.
Twilio SMS is on the roadmap.
To send an SMS programmatically – for example, inside a FunctionHandler or in a scheduler:
import gramex
notifier = gramex.service.sms['amazon'] # Available only if Gramex is running
# Or, to construct the Notifier when using Gramex as a library, use:
# from gramex.services import AmazonSNS
# notifier = AmazonSNS(aws_access_key_id, aws_access_secret_key, region_name)
result = notifier.send(
to='+919741552552', # International mobile number
subject='Message to send', # Text message to send
sender='Gramex', # Optional sender identifier
)
The result has the API response which contains additional information about
the SMS. To fetch its delivery status, use:
status = notifier.status(result) # Not available for Amazon SNS
This returns the API response for the delivery status.
Here is a sample form to send messages:
Note: Depending on the geography, the “Sender” field may not be sent. In India, it is not. The sender is replaced with “HP-Notice” or similar messages. In Singapore, the Sender field is used as-is.