Package: telstra-messaging
Telstra MessagingThe SDK for the Telstra Messaging API enables you to send and receive messages
to Australian mobile numbers. For more information about this product, please
see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverview?version=3.x.:warning:This SDK is experimental, everything is subject to changeInstallingpipinstalltelstra.messagingGetting StartedSet theTELSTRA_CLIENT_IDandTELSTRA_CLIENT_SECRETenvironment variables. These
are theClient idandClient secretyou can find here:https://dev.telstra.com/user/me/apps.To send your first message:fromtelstra.messagingimportmessagemessage.send(to="+61412345678",from_="privateNumber",message_content="Hello from Python Messaging SDK!")To set the required environment variables if your application is inapp.py:TELSTRA_CLIENT_ID"<client id>"TELSTRA_CLIENT_SECRET="<client secret>"pythonapp.pyAuthenticationAuthentication through environment variables, a shared credentials file
and through code are supported.Authentication using environment variablesExport the following two environment variables, replacing the values
with your own credentials.exportTELSTRA_CLIENT_ID="<client id>"exportTELSTRA_CLIENT_SECRET="<client secret>"Authentication using shared credentialsCreate a~/.telstra/credentialsfile in your home path with the following contents,
replacing the values with your own credentials.[default]
TELSTRA_CLIENT_ID = <client id>
TELSTRA_CLIENT_SECRET = <client secret>Authentication using codeOn top of the authentication through theTELSTRA_CLIENT_IDandTELSTRA_CLIENT_SECRETenvironment variables
and a~/.telstra/credentialsshared credentials
file, authentication through code is also supported.
For example:fromtelstra.messaging.utils.configimportCONFIGCONFIG.telstra_client_id='<client id>'CONFIG.telstra_client_secret='<client secret>'This should be done before any interactions requiring authentication, such as
sending a message.Free TrialTelstra offers a free trial for the messaging API to help you evaluate whether
it meets your needs. There are some restrictions that apply compared to the
full API, including a maximum number of messages that can be sent and requiring the
registration of a limited number of destinations before a message can be sent
to that destination. For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#FreeTrial.Registering Free Trial Numbers:information_source:Only required for the free trialRegister numbers for the free trial. For more information, please see
here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#RegisteraFreeTrialNumber.The functiontelstra.messaging.free_trial_numbers.createcan be used to register
destinations. It takes the following arguments:phone_numbers: A list of destinations, expected to be phone numbers
of the form04XXXXXXXX.Raisestelstra.messaging.exceptions.FreeTrialNumbersErrorif anything goes wrong.It returns the list of phone numbers that have been registered.For example:# Register free trial numbersfromtelstra.messagingimportfree_trial_numbersphone_numbers=free_trial_numbers.create(phone_numbers=["+61412345678"])print(phone_numbers)Fetch all Free Trial Numbers:information_source:Only required for the free trialFetch the Free Trial Number(s) currently assigned to your account. For more information,
please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#FetchyourFreeTrialNumbers.The functiontelstra.messaging.free_trial_numbers.get_allcan be used to retrieve registered destinations.
It takes no arguments.Raisestelstra.messaging.exceptions.FreeTrialNumbersErrorif anything goes wrong.It returns the list of phone numbers that
have been registered.For example:# Get all free trial numbersfromtelstra.messagingimportfree_trial_numbersphone_numbers=free_trial_numbers.get_all()print(phone_numbers)Virtual NumberGives you a dedicated mobile number tied to an application which
enables you to receive replies from your customers. For more information,
please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#VirtualNumbers.Assign Virtual NumberWhen a recipient receives your message, you can choose whether they'll see a privateNumber,
Virtual Number or senderName (paid plans only) in the from field.
If you want to use a Virtual Number, use this function to assign one.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#AssignaVirtualNumber.The functiontelstra.messaging.virtual_number.createcan be used to create a
numbers. It takes the following arguments:reply_callback_url(optional): The URL that replies to the
Virtual Number will be posted to.tags(optional): Create your own tags and use them to fetch,
sort and report on your Virtual Numbers through our other endpoints.
You can assign up to 10 tags per number.Raisestelstra.messaging.exceptions.VirtualNumbersErrorif anything goes wrong.It returns an object with the following properties:virtual_number: The Virtual Number assigned to your account.last_use: The last time the Virtual Number was used to send a message.reply_callback_url: The URL that replies to the
Virtual Number will be posted to.tags: Any customisable tags assigned to the Virtual Number.For example:# Assign a virtual numberfromtelstra.messagingimportvirtual_numberassigned_numbers=virtual_number.assign()print(assigned_numbers)Fetch a Virtual NumberFetch the tags, replyCallbackUrl and lastUse date for a Virtual Number.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#FetchaVirtualNumber.The functiontelstra.messaging.virtual_number.getcan be used to get the current
number. It takes the following arguments:virtual_number: The Virtual Number assigned to your account.Raisestelstra.messaging.exceptions.VirtualNumbersErrorif anything goes wrong.It returns an object with the following
properties:virtual_number: The Virtual Number assigned to your account.last_use: The last time the Virtual Number was used to send a message.reply_callback_url: The URL that replies to the
Virtual Number will be posted to.tags: Any customisable tags assigned to the Virtual Number.For example:# Get a virtual numberfromtelstra.messagingimportvirtual_numberretrieved_number=virtual_number.get(virtual_number="0400000001")print(retrieved_number)Fetch all Virtual NumbersFetch all Virtual Numbers currently assigned to your account.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#FetchallVirtualNumbers.The functiontelstra.messaging.virtual_number.get_allcan be used to
get the all virtual numbers associated to your account.
It takes the following arguments:limit: Tell us how many results you want us to return, up to a maximum of 50.offset: Use the offset to navigate between the response results.
An offset of 0 will display the first page of results, and so on.filter: Filter your Virtual Numbers by tag or by number.Raisestelstra.messaging.exceptions.VirtualNumbersErrorif anything goes wrong.It returns an object with the following
properties:virtual_numbers: A list of Virtual Numbers assigned to your account.paging: Paging information.For example:# Get all virtual numbersfromtelstra.messagingimportvirtual_numberretrieved_virtual_numbers=virtual_number.get_all()print(retrieved_virtual_numbers)Update a Virtual NumberUpdate a virtual number attributes. For more information,
please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#UpdateaVirtualNumber.The functiontelstra.messaging.virtual_number.updatecan be used to update a
virtual number. It takes the following arguments:virtual_number: The Virtual Number assigned to your account.reply_callback_url(optional): The URL that replies to the
Virtual Number will be posted to.tags(optional): Create your own tags and use them to fetch,
sort and report on your Virtual Numbers through our other endpoints.
You can assign up to 10 tags per number.Raisestelstra.messaging.exceptions.VirtualNumbersErrorif anything goes wrong.It returns an object with the following properties:virtual_number: The Virtual Number assigned to your account.last_use: The last time the Virtual Number was used to send a message.reply_callback_url: The URL that replies to the
Virtual Number will be posted to.tags: Any customisable tags assigned to the Virtual Number.For example:# Update a virtual numberfromtelstra.messagingimportvirtual_numberupdated_virtual_number=virtual_number.update(virtual_number="0400000001",tags=["sdk","v3"])print(updated_virtual_number)Delete Virtual NumberDelete the a virtual number. For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#DeleteaVirtualNumber.The functiontelstra.messaging.virtual_number.deletecan be used
to delete the current number. It takes the following arguments:virtual_number: The Virtual Number assigned to your account.Raisestelstra.messaging.exceptions.VirtualNumbersErrorif anything goes wrong.It returns nothing.# Delete a virtual numberfromtelstra.messagingimportvirtual_numbervirtual_number.delete(virtual_number="0400000001")Fetch all Recipient Optouts listFetch any mobile number(s) that have opted out of receiving messages
from a Virtual Number assigned to your account.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Fetchallrecipientoptoutslist.The functiontelstra.messaging.virtual_number.get_optoutscan be used to
get the list of mobile numbers that have opted out of receiving messages
from a virtual number associated to your account.
It takes the following arguments:virtual_number: The Virtual Number assigned to your account.limit: Tell us how many results you want us to return, up to a maximum of 50.offset: Use the offset to navigate between the response results.
An offset of 0 will display the first page of results, and so on.Raisestelstra.messaging.exceptions.VirtualNumbersErrorif anything goes wrong.It returns an object with the following
properties:recipient_optouts: A list of recipient optouts.paging: Paging information.For example:# Get all virtual numbersfromtelstra.messagingimportvirtual_numberretrieved_optout_numbers=virtual_number.get_optouts(virtual_number="0400000001")print(retrieved_optout_numbers)MessageSend and receive messages. For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Messages.Send MessageSend a message to a mobile number, or to multiple mobile numbers.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#SendanSMSorMMS.The functiontelstra.messaging.message.sendcan be used to send a message.
It takes the following arguments:to: The destination address, expected to be a phone number of the form+614XXXXXXXXor04XXXXXXXX.from_: This will be either "privateNumber", one of your Virtual Numbers
or your senderName.message_content(Either one of messageContent or multimedia is required).
The content of the message.multimedia(Either one of messageContent or multimedia is required).
MMS multimedia content.retry_timeout(optional): How many minutes you asked the server to
keep trying to send the message.schedule_send(optional): The time (in Central Standard Time)
the message is scheduled to send.delivery_notification(optional): If set to true, you will receive
a notification to the statusCallbackUrl when your SMS or MMS
is delivered (paid feature).status_callback_url(optional): The URL the API will call when the
status of the message changes.tags(optional): Any customisable tags assigned to the message.The dataclasstelstra.messaging.message.Multimediacan be used to build an mms payload.
It takes the following arguments:type: The content type of the attachment, for exampleimage/png.filename(optional): Optional field, for exampleimage.png.payload: The payload of an mms encoded as base64.Raisestelstra.messaging.exceptions.MessageErrorif anything goes wrong.It returns an object with the following properties:messageId: Use this UUID with our other endpoints to fetch,
update or delete the message.status: The status will be either queued, sent, delivered or expired.to: The recipient's mobile number(s).from: This will be either "privateNumber", one of your
Virtual Numbers or your senderName.message_content: The content of the message.multimedia: The multimedia content of the message (MMS only).retry_timeout: How many minutes you asked the server to keep
trying to send the message.schedule_send: The time (in Central Standard Time) a message
is scheduled to send.delivery_notification: If set to true, you will receive a notification to the
statusCallbackUrl when your SMS or MMS is delivered (paid feature).status_callback_url: The URL the API will call when the status of
the message changes.tags: Any customisable tags assigned to the message.For example:# Send an SMSfromtelstra.messagingimportmessagemessage.send(to="+61412345678",from_="privateNumber",message_content="Hello from Python Messaging SDK!")# Send an MMSmessage.send(to="+61412345678",from_="privateNumber",multimedia=[message.Multimedia(type="image/jpeg",fileName="myFile.jpeg",payload="iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==",)],)Get a MessageUse the messageId to fetch a message that's been sent from/to
your account within the last 30 days.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Fetchamessage.The functiontelstra.messaging.message.getcan be used to retrieve
the a message. It takes the following arguments:message_id:Unique identifier for the message.Raisestelstra.messaging.exceptions.MessageErrorif anything goes wrong.It returns an object with the following properties:message_id: Use this UUID with our other endpoints to fetch,
update or delete the message.status: The status will be either queued, sent, delivered or expired.create_timestamp: The time you submitted the message to
the queue for sending.sent_timestamp: The time the message was sent from the server.received_timestamp: The time the message was received by the
recipient's device.to: The recipient's mobile number(s).from: This will be either "privateNumber", one of your
Virtual Numbers or your senderName.message_content: The content of the message.multimedia: The multimedia content of the message (MMS only).direction: Direction of the message (outgoing or incoming).retry_timeout: How many minutes you asked the server to keep
trying to send the message.schedule_send: The time (in Central Standard Time) the message is
scheduled to send.delivery_notification: If set to true, you will receive a notification
to the statusCallbackUrl when your SMS or MMS is delivered (paid feature).status_callback_url: The URL the API will call when the status of
the message changes.queue_priority: The priority assigned to the message.tags: Any customisable tags assigned to the message.For example:# Get a messagefromtelstra.messagingimportmessagesent_message=message.send(to="+61412345678",from_="privateNumber",message_content="Hello from Python Messaging SDK!")message=message.get(message_id=sent_message.message_id)print(message)Get all MessagesFetch messages that have been sent from/to your account in the last 30 days.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Fetchallsent/receivedmessages.The functiontelstra.messaging.message.get_allcan be used to fetch
all messages. It takes the
following arguments:limit: Tell us how many results you want us to return, up to a maximum of 50.offset: Use the offset to navigate between the response results.
An offset of 0 will display the first page of results, and so on.filter: Filter your Virtual Numbers by tag or by number.Raisestelstra.messaging.exceptions.MessageErrorif anything goes wrong.It returnsNoneif there are no more replies or an object with the
following properties:messages: List of all messages.paging: Paging information.For example:# Get all messagesfromtelstra.messagingimportmessagereply=message.get_all(limit=5,offset=0,filter="Python,SDK")print(reply)Update a MessageUpdate a message that's scheduled for sending, you can change any of
the below parameters, as long as the message hasn't been sent yet.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Updateamessage.The functiontelstra.messaging.message.updatecan be used to update a message.
It takes the following arguments:to: The destination address, expected to be a phone number of the form+614XXXXXXXXor04XXXXXXXX.from_: This will be either "privateNumber", one of your
Virtual Numbers or your senderName.message_contentThe content of the message.
Either one of messageContent or multimedia is required.multimediaMMS multimedia content.retry_timeout(optional): How many minutes you asked the server to
keep trying to send the message.schedule_send(optional): The time (in Central Standard Time) the
message is scheduled to send.delivery_notification(optional): If set to true, you will receive a
notification to the statusCallbackUrl when your SMS or MMS is delivered (paid feature).status_callback_url(optional): The URL the API will call when
the status of the message changes.tags(optional): Any customisable tags assigned to the message.Raisestelstra.messaging.exceptions.MessageErrorif anything goes wrong.The dataclasstelstra.messaging.message.Multimediacan be used to build
a mms payload. It takes the following arguments:type: The content type of the attachment, for exampleimage/png.filename(optional): Optional field, for exampleimage.png.payload: The payload of an mms encoded as base64.Raisestelstra.messaging.exceptions.MessageErrorif anything goes wrong.It returns an object with the following properties:messageId: Use this UUID with our other endpoints to fetch,
update or delete the message.status: The status will be either queued, sent, delivered or expired.to: The recipient's mobile number(s).from: This will be either "privateNumber", one of your
Virtual Numbers or your senderName.message_content: The content of the message.multimedia: The multimedia content of the message (MMS only).retry_timeout: How many minutes you asked the server to keep trying
to send the message.schedule_send: The time (in Central Standard Time) the message
is scheduled to send.delivery_notification: If set to true, you will receive a notification to the
statusCallbackUrl when your SMS or MMS is delivered (paid feature).status_callback_url: The URL the API will call when the
status of the message changes.tags: Any customisable tags assigned to the message.For example:# Update a messagefromtelstra.messagingimportmessagemessage.update(message_id="8540d774-4863-4d2b-b788-4ecb19412e85",to="+61412345678",from_="privateNumber",message_content="Hello from Python Messaging SDK!")Update Message TagsUpdate message tags, you can update them even after your message has been delivered.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Updatemessagetags.The functiontelstra.messaging.message.update_tagscan be used to
update message tags. It takes the following arguments:message_id:Unique identifier for the message.tags(optional): Any customisable tags assigned to the message.Raisestelstra.messaging.exceptions.MessageErrorif anything goes wrong.It returns nothing.For example:# Update message tagsfromtelstra.messagingimportmessagemessage.update_tags(message_id="8540d774-4863-4d2b-b788-4ecb19412e85",tags=["Python","V3"])Delete a MessageDelete a scheduled message, but hasn't yet sent.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Deleteamessage.The functiontelstra.messaging.message.deletecan be used to delete a message.
It takes the following arguments:message_id: Unique identifier for the message.Raisestelstra.messaging.exceptions.MessageErrorif anything goes wrong.It returns nothing.For example:# Delete a messagefromtelstra.messagingimportmessagemessage.delete(message_id="8540d774-4863-4d2b-b788-4ecb19412e85")ReportsCreate and fetch reports. For more information, please see here:https://dev.telstra.com/content/messaging-api-v3#tag/reports.Request a ReportRequest a CSV report of messages (both incoming and outgoing)
that have been sent to/from your account within the last three months.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Submitarequestforamessagesreport.The functiontelstra.messaging.reports.createcan be used to create a report.
It takes the following arguments:start_date: Set the time period you want to generate a
report for by typing the start date (inclusive) here. Note that we only
retain data for three months, so please ensure your startDate is not more
than three months old. Use ISO format(yyyy-mm-dd), e.g. "2019-08-24".end_date: Type the end date (inclusive) of your reporting period here.
Your endDate must be a date in the past, and less than three months from your startDate.
Use ISO format(yyyy-mm-dd), e.g. "2019-08-24".report_callback_url: The callbackUrl where notification
is sent when report is ready for download.filter_: Filter report messages by -
tag - use one of the tags assigned to your message(s)
number - either the Virtual Number used to send the message,
or the Recipient Number the message was sent to.Raisestelstra.messaging.exceptions.ReportsErrorif anything goes wrong.It returns an object with the following properties:report_id: Use this UUID with our other endpoints to fetch the report.report_callback_url: If you provided a reportCallbackUrl in your request,
it will be returned here.report_status: The status of the report. It will be either:queued – the report is in the queue for generation.completed – the report is ready for download.failed – the report failed to generate, please try again.For example:# Create a reportfromtelstra.messagingimportreportsreports_create_response=reports.create(start_date="2023-03-15",end_date="2023-03-30",filter_="0412345678")Fetch a specific reportUse the report_id to fetch a download link for a report generated.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#FetchaReport.The functiontelstra.messaging.reports.getcan be used to retrieve
the a report download link. It takes the following arguments:report_id:Unique identifier for the report.Raisestelstra.messaging.exceptions.ReportsErrorif anything goes wrong.It returns an object with the following properties:report_id: Use this UUID with our other endpoints to fetch the report.report_status: The status of the report.report_url: Download link to download the CSV file.For example:# Get a report download linkfromtelstra.messagingimportreportsreport_response=reports.get(report_id='6940c774-4335-4d2b-b758-4ecb19412e85')print(report_response)Fetch all reportsFetch details of all reports recently generated for your account.
Use it to check the status of a report, plus fetch the report ID,
status, report type and expiry date.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#Fetchallreports.The functiontelstra.messaging.reports.get_allcan be used to fetch
all reports. It doesn't take any arguments.Raisestelstra.messaging.exceptions.ReportsErrorif anything goes wrong.It returns a list of objects with the following properties:report_id: Use this UUID with our other endpoints to fetch the report.report_status: The status of the report.report_type: The type of report generated.report_expiry: The expiry date of your report. After this date,
you will be unable to download your report.For example:# Get all reportsfromtelstra.messagingimportreportsreply=reports.get_all()print(reply)Health CheckGet operational status of the messaging serviceCheck the operational status of the messaging service.
For more information, please see here:https://dev.telstra.com/docs/messaging-api/apiReference/apiReferenceOverviewEndpoints?version=3.x#HealthCheck.The functiontelstra.messaging.health_check.getcan be used to get the status.
It takes no arguments.Raisestelstra.messaging.exceptions.HealthCheckErrorif anything goes wrong.It returns nothing.For example:# Get health checkfromtelstra.messagingimporthealth_checktry:health_check.get()exceptHealthCheckErroraseprint(e)ExceptionsAll exceptions that can be raised derive fromMessagingBaseException:fromtelstra.messaging.exceptionsimportMessagingBaseException