Showing posts with label ideamart. Show all posts
Showing posts with label ideamart. Show all posts
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

IdeaMart

Ideamart is a platform presented by Dialog Axiata PLC to developers and content providers to use the Dialog network based features via shared APIs and monetize their efforts.

Hope you have an idea about what ideamart is. ideamart is a vast popular platform these days. I have seen lot of television advertisements also. But in the mean time I was unable to find any web application (website) which have  used ideamart APIs. So this tutorial will guide you how to use ideamart APIs in a web application. I am using basic java, JSP, servlet for this tutorial. You can extend it to any other framework like spring, vaadin, etc. This tutorial will guide you to implement following scenarios.
  • Receiving a message
  • Sending a message
  • Displaying received messages in a html page     
Prerequisites 
  • java 1.8
  • ideamart simulator 
  • apache tomcat 8
  • apache maven 3
  • IDE  (I am using intellij idea) 
Hope you are familiar with the above stuff. First you need to download or clone the repository. 
after downloading it you can open your favorite IDE and import the pom.xml file. It will download all the dependencies. To run the program you have to configure apache tomcat to your IDE 

How to configure apache tomcat in IntelliJ IDEA

Implementation 

Below image is displaying the source code structure after all the above configurations.
     
receive - This package is to handle the receiving messages. To receive a message and to send a message you need a library given by the ideamart. It is included in the resources directory. By implementing the MoSmsListener class you can handle the receiving messages. I hope you can easily understand it when you look at the Receive class implementation. receiving message URL is configured in the web.xml file. 

operations - when you receive a message you can do any operation for that. In this tutorial I am splitting it and put it into a hashmap. You can do any other operation you need.    

send - This package is to send messages to particular number. SmsRequestSender class can be used for that. 

db - This package is used for database operations. For this implementation i am using a hashmap. You can use any other database like sql or nosql. 

I hope you got some idea about the implementation. Now you are free to run the application. Before you run the program please make sure that all the libraries are included into the artifacts.
For that If you use intellij idea press Shift+ CTRL + ALT + S and then you can see the project structure.
It looks like below.
In the above picture both selected library and the other one are not in the artifacts. To add those into it right click on it and select "put into WEB-INF/lib" 
Then you can start the server and the ideamart simulator. 
You can see the index page and ideamart simulator like below.

                                                                                                             

URL - http://localhost:8080/Receiver
This URL is configured in the web.xml. In this tutorial I am going to get the name and the message with application name as a message. So your message format should have above three details.
ex - <app name> tharinda mymessage

Now you can send this message and wait for the response. When the response message came reload the index page and you will get the result below.
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 contact me go to my website.

Gknow is SinGlish USSD app which the users will answer gen. knowledge, Math and logic questions. And Compete with all users to become the top in the leader board. We have done this for dialog kandy app challenge.

Team members

Tharinda Ehelepola
Namodaya yasas
Thisaru Guruge
Lahiru Srimal

Actually we spends 2 sleepless nights for done this. The initial idea was came from my mind.  

What is Gknow?


  • SinGlishUSSD app.
  • Users will answer general knowledge, Mathematics and logical questions.
  • Compete with other users to rise up in the leader board to become the master Braininess.

What GKnow does?
  • Providing a platform to gain general knowledge.
  • Improving math and logic thinking.
  • Providing an opportunity to compete with other braininess.
  • Creating effective way to spend your leisure time.  
Technical part of Gknow

       We used PHP and mysql as programing languages. Read user inputs and send them to MySQL database using PHP.  And also Send the response to the users returned by MySQL queries.
       First we drew a small ER diagram. According to the ER diagram database has a table named “user” with columns for name and unique phone number.  Questions table to store questions and answers and Answered table to store answered questions numbers. Also used Triggers and MySQL functions.

dial #771*910 on dialog mobiles. Try it yourself.

Facebook page

https://www.facebook.com/gknowpera


Github

https://github.com/tharinda221/Gknow






Powered by Blogger.