How to send sms message to phone

Hi Django Developer,

I want to the user enter her/his phobne number and text message. So what I do:

In sms_page.html, the user enter the number and write the message. and then submit to go through the next page done.html that

message send successful :slight_smile:

<!-- ========= sms_page.html =============== -->

<form action="/done" method="get">
        <nav class="bg-light">
          <div class="mb-3">
            <label for="inputPassword" class="col-sm-2 col-form-label"><h5>Enter Phone Number</h5></label>
            <div class="col-sm-10">
              <input type="text" class="form-control" id="inputPhone" name="phone">
          </div>
        </nav>

        <nav class="bg-light">
          <div class="mb-3">
            <label for="exampleFormControlTextarea1" class="form-label"><h5>Enter Your Text</h5></label>
            <textarea class="form-control" id="exampleFormControlTextarea1" rows="3" name="text"></textarea>
          </div>
        </nav>

        <center>
          <button type="submit" class="btn btn-primary btn-lg"><h4>Send Message</h4></button>
        </center>
    </form>

After that I get the input from sms_page.html and sent to done.html by using views.py

def done_page(request):
    phone = request.GET.get('phone')
    text = request.GET.get('text')

return render(request, "en/done.html" , {'phn':phone , 'txt':text})

the result appear on done.html:

Now, I would like to using custom of api for sent the message:
http://www.4jawaly.net/api/sendsms.php?username=XXXXXX&password=XXXX&message=HelloTest&numbers=05XXXXX&sender=XXXX&unicode=e&Rmduplicated=0&return=Json

These function that can that set all parameter api and posted. But I’m not sure Is that correct?

def sendPostRequest( req_URL ,API_key , secret_API , text_message , phone_no , sender):

    req_param = {
        'apikey' : API_key, 
        'secretKey' :secret_API, 
        'message' :text_message ,
        'phone' :phone_no, 
        'from': sender,   
    }

    return requests.post(req_URL , req_param)

The last thing, I added to sent post the done.html:

def done_page(request):
    phone = request.GET.get('phone')
    text = request.GET.get('text')

    t_phone = 'numbers='+ f'{phone}' + '&'
    t_text = 'message=' + f'{text}' + '&'
 
    # get resposnse 
    sendPostRequest(
        URL ,'username=XXXXXX' , 'password=XXXXXXX' , t_text
        , t_phone,'sender=Matager&unicode=e&Rmduplicated=0&return=Json')
return render(request, "en/done.html" , {'phn':phone , 'txt':text})

I have browsed through many tutorials on sms-gateways/carriers. But I am lost. Can you please guide me on how to sent SMS API in Django ?

Hello @Shathamhb!
I’ve worked with SMS messages with Django, but not with the platform you’re using (4jawaly.net)
What I can see from your code is that you’re sending a POST request with the data as a POST data, and not a GET data.
I explain myself:
From what I understand, you need to send a request to http://www.4jawaly.net/api/sendsms.php?username=XXXXXX&password=XXXX&message=HelloTest&numbers=05XXXXX&sender=XXXX&unicode=e&Rmduplicated=0&return=Json with the USERNAME, PASSWORD, NUMBERS and SENDER right? As you can see in the URL, this is a GET Request.
But in the “sendPostRequest()” function you’re returning a “requests.post” instead a “requests.get”, as also the data you’re sending is like a form data, and not URL parameters data.
Another thing that I could recommend you is to always check the status code the request is returning, because there always could be issues with the communication, your service, or the provider service, so you can handle in a better way those kind of errors.
Let me know if I can help in anything else
Greetings!