Implement IdeaMart SMS application within few minutes using python

/
1 Comments
I wrote my previous article about python and python flask. It is needed for this tutorial. So if you are not followed it yet link is [1].
If you already familiar with python and if you already setup your machine for it no need of following it. As mentioned in the title I am going to show you how to implement simple SMS application within few minutes.
First of all it's better to download or clone the source code by using [2] URL.

Now you can see the project structure is same as my previous tutorial. So open the terminal in the location of above source code and type below command.
python app.py

Now server should start if you followed above steps correctly.
After this, You need to start IdeaMart simulator on your machine. Hope you already aware of it. If not you can go through my previous tutorial using [3] link.
After you run IdeaMart simulator using [4] URL, Open the SMS tab and add [5] to URL box in Application data.
Now it's time to test the application. You can send sms with your full name.
Ex - Tharinda Dilshan

Then the results should be appeared.

Now let's move into the implementation.

import logging
import urllib2

from flask import *

app = Flask(__name__)
app.debug = True


@app.route('/')
def hello_world():
    return render_template("index.html")


@app.route('/smsReceiver', methods=["GET", "POST"])
def sms_receiver():
    """
    This method is to retrieve request came from ideamart simulator of dialog and send sms message
    :return:
    """
    if request.method == "GET":
        response = make_response("Telco App is running")
        response.headers['Content-Type'] = 'application/json'
        response.headers['Accept'] = 'application/json'
        return response
    else:
        ideamart_message = json.loads(request.data)
        name = ideamart_message["message"].split(" ")[1]
        res = {'message': "Hi, " + name,
               "destinationAddresses": ideamart_message["sourceAddress"],
               "password": "password",  # This should be replaced with your ideamart app password
               "applicationId": ideamart_message["applicationId"]
               }

        # URL should be  changed to https://api.dialog.lk/sms/send when you host the application
        url = "http://localhost:7000/sms/send"
        req = urllib2.Request(url, data=json.dumps(res),
                              headers={"Content-Type": "application/json", "Accept": "application/json"})
        response = urllib2.urlopen(req)
        ideamart_respones = response.read()
        logging.error("Result content: " + ideamart_respones)

        if response.getcode() == 200:
            logging.info('*** Message delivered Successfully! ****')
            response = make_response("Message delivered Successfully!")
            response.headers['Content-Type'] = 'application/json'
            response.headers['Accept'] = 'application/json'
            return response
        else:
            logging.error(
                '*** Message was not delivered Successfully!! ERROR-CODE: ' + str(response.getcode()) + ' ****')
            response = make_response("Message was not delivered Successfully!")
            response.headers['Content-Type'] = 'application/json'
            response.headers['Accept'] = 'application/json'
            return response


if __name__ == '__main__':
    app.run(host="localhost", port=5000)


Above code is used to run the whole application. Basic functions related to flask framework is explained in my previous tutorial. Hope now you aware of it. When you click send button in ideamart simulator it will send a post request to the URL [5] that is mentioned above. As you can see in the program that request is handled by the function named as sms_receiver. The request message is taken as a variable called ideamart_message. After that, It is split into parts and send the response back to URL [6].

[6] URL should be changed when you host this application. The request should be send as a JSON request and headers should be added as you can see in the program. After sending the request, The response message which will be sent by the simulator can be seen as a log message in the running program console.

That's it! Hope this will help you.
Thanks for reading :)

If you have any problem, feel free to leave a comment and I appreciate any feedback.

For more info and to contact me go to my website.

[1] - https://tharindaehelepola.blogspot.com/2016/05/simple-web-application-using-python.html
[2] - https://github.com/tharinda221/ideamart-sms-application-python
[3] - https://tharindaehelepola.blogspot.com/2015/12/ideamart-simple-application-in-java.html
[4] - http://localhost:10001/
[5] - http://localhost:5000/smsReceiver
[6] - http://localhost:7000/sms/send



You may also like

1 comment :

  1. Extremely inspired! Everything is extremely open and clear illumination of issues. It contains really certainties. Your site is extremely important. Much obliged for sharing. sms marketing in bangladesh

    ReplyDelete

Powered by Blogger.