<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Foysal</title>
    <description>The latest articles on DEV Community by Foysal (@foysal365).</description>
    <link>https://dev.to/foysal365</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1502229%2F6ff00439-7a57-4cb2-8d81-b4814663fe6e.png</url>
      <title>DEV Community: Foysal</title>
      <link>https://dev.to/foysal365</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/foysal365"/>
    <language>en</language>
    <item>
      <title>Sending SMS Messages with Python and ClickSend</title>
      <dc:creator>Foysal</dc:creator>
      <pubDate>Thu, 16 May 2024 22:34:57 +0000</pubDate>
      <link>https://dev.to/foysal365/sending-sms-messages-with-python-and-clicksend-phl</link>
      <guid>https://dev.to/foysal365/sending-sms-messages-with-python-and-clicksend-phl</guid>
      <description>&lt;p&gt;Installation&lt;/p&gt;

&lt;p&gt;To start sending, all you need to do is install the ClickSend Client. Pick your preferred method below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Install via Pip&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install clicksend-client&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Below is the full snippet you can use to send an SMS. It includes placeholders that will need to be replaced when implementing the code in your project.&lt;br&gt;
&lt;code&gt;&lt;br&gt;
import clicksend_client&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;**Send your first message&lt;br&gt;
**Below is the full snippet you can use to send an SMS. It includes placeholders that will need to be replaced when implementing the code in your project.&lt;/p&gt;

&lt;p&gt;USERNAME &amp;gt; Your ClickSend Username. Find it here.&lt;/p&gt;

&lt;p&gt;API_KEY &amp;gt; Your ClickSend API Key. Find it here.&lt;/p&gt;

&lt;p&gt;SOURCE &amp;gt; The origin identifier for your API request, which could be the name of your application or the source location for the request.&lt;/p&gt;

&lt;p&gt;MESSAGE &amp;gt; The content of your SMS message.&lt;/p&gt;

&lt;p&gt;TO_PHONE_NUMBER &amp;gt; The phone number you're sending the message to, including the country code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from __future__ import print_function
import clicksend_client
from clicksend_client import SmsMessage
from clicksend_client.rest import ApiException

# Configure HTTP basic authorization: BasicAuth
configuration = clicksend_client.Configuration()
configuration.username = 'USERNAME'
configuration.password = 'API_KEY'

# Create an instance of the API class
api_instance = clicksend_client.SMSApi(clicksend_client.ApiClient(configuration))

# Configure your message
sms_message = SmsMessage(
    source="SOURCE",  # Replace this with your desired source name
    body="MESSAGE", # Write your message here
    to="TO_PHONE_NUMBER" # Enter the number you are sending to
)

sms_messages = clicksend_client.SmsMessageCollection(messages=[sms_message])


try:
    # Send an SMS message(s)
    api_response = api_instance.sms_send_post(sms_messages)
    print(api_response)
except ApiException as e:
    print("Exception when calling SMSApi-&amp;gt;sms_send_post: %s\n" % e)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The code explained&lt;/strong&gt;&lt;br&gt;
Import functions&lt;br&gt;
Next, set up your Username and API Key so you can use the ClickSend API in your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from __future__ import print_function
import clicksend_client
from clicksend_client import SmsMessage
from clicksend_client.rest import ApiException
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Authorisation&lt;/strong&gt;&lt;br&gt;
Next, add your API credentials so you can use the ClickSend API in your project.&lt;/p&gt;

&lt;p&gt;The following line of code will create an instance of the ACCOUNTAPI class, which is configured to use the APICLIENT with the credentials that we set up in the previous step. This instance will allow you to interact with the ClickSend API's account-related functionality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;configuration = clicksend_client.Configuration()
configuration.username = 'USERNAME'
configuration.password = 'API_KEY'
Create an instance of the API class
Next, configure the content of your message and define who you’re sending to-and-from.

api_instance = clicksend_client.SMSApi(clicksend_client.ApiClient(configuration))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure your message&lt;br&gt;
For testing purposes, we recommend you start by sending to your own number.&lt;/p&gt;

&lt;p&gt;For additional properties that you can use here, see the Full API Reference.&lt;/p&gt;

&lt;p&gt;Replace SOURCE with your preferred source (eg. the name of your application). This is not seen by recipients, but will help you to identify messages sent from various applications.&lt;br&gt;
Replace MESSAGE with your own message.&lt;br&gt;
Replace TO_PHONE_NUMBER with your own mobile number, including country code. For example, if you have an Australian number, it will start with +61.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sms_message = SmsMessage(
    source="SOURCE",
    body="MESSAGE",
    to="TO_PHONE_NUMBER"
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Send SMS message&lt;/strong&gt;&lt;br&gt;
If an exception occurs during the API call, it catches the APIEXCEPTION and prints an error message with the exception details. This structure helps you handle both successful API calls and potential errors that might occur during the process.&lt;/p&gt;

&lt;p&gt;If an exception occurs during the API call, it catches the ApiException and prints an error message with the exception details. This structure helps you handle both successful API calls and potential errors that might occur during the process.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try:
    api_response = api_instance.sms_send_post(sms_messages)
    print(api_response)
except ApiException as e:
    print("Exception when calling SMSApi-&amp;gt;sms_send_post: %s\n" % e)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/iamfoysal"&gt;Follow me.&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>sms</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
