Smile SN SMS API – Python Version

Hello Again

Below is the python code of smilesn sms API, They usally provide the sample code in PHP, So i rewrote it in Python, Thought of sharing it as it might save someones day. Smilesn saves the session in a text file and then uses it to send and retrieve smss. Below is the Code.

import requests
import ast

class smile_api:

	def get_session(self):
		username = 'username'   #write your provided username
		password = 'password'   #write your provided password

		r = requests.get("http://api.smilesn.com/session?username="+str(username)+"&password="+str(password))
		responce = r.content

		resp_dict = ast.literal_eval(responce)
		print  resp_dict
		sessionID = resp_dict['sessionid']
		print sessionID

		f1 = open("session.txt","w")
		f1.write(sessionID)
		f1.close()

		return sessionID

	def send_sms(self, receversNumber=None, sendersNumber=None, textMessage=None):

		sessionFile  = open("session.txt","r+")
		sessionId = sessionFile.read();

		print 'Content of Session File ' + str(sessionId)
		sendersNumber=SendersNumber    #Here Optionaly and statically you can provide the senders Number, It will be a four digit number like 8822 or 8883 or you can provide one in parameters

		if not(sessionId):
			print 'Retrying to Aquire Session'
			sessionId = self.get_session()

		result = requests.get("http://api.smilesn.com/sendsms?sid="+str(sessionId)+"&receivenum="+str(receversNumber)+"&sendernum="+str(sendersNumber)+"&textmessage="+str(textMessage))

		resp_dict = ast.literal_eval(result.content)
		session_status = resp_dict['status']
		print session_status

		if session_status == "SESSION_EXPIRED":
			print 'SESSION_EXPIRED, Retrying to Aquire Session'
			sessionId = self.get_session()
			result = requests.get("http://api.smilesn.com/sendsms?sid="+str(sessionId)+"&receivenum="+str(receversNumber)+"&sendernum="+str(sendersNumber)+"&textmessage="+str(textMessage))

		return result

	def receive_sms(self):
		sessionFile  = open("session.txt","r+")
		sessionId = sessionFile.read();
		print 'Content of Session File ' + str(sessionId)

		if not(sessionId):
			print 'Retrying to Aquire Session'
			sessionId = self.get_session()

		result = requests.get("http://api.smilesn.com/sendsms?sid="+str(sessionId))

		resp_dict = ast.literal_eval(result.content)
		session_status = resp_dict['status']
		print session_status

		if session_status == "SESSION_EXPIRED":
			print 'SESSION_EXPIRED, Retrying to Aquire Session'
			sessionId = self.get_session()
			data = requests.get("http://api.smilesn.com/sendsms?sid="+str(sessionId))

		return data

api = smile_api()
api.send_sms('MobileNumber', 'SendersNumber', 'Hello, Just testing the API')

I hope it will be helpful, Let me know if there is any problem

Regards

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s