Tuesday, March 12, 2013

Sending SMS from third party application using Webservices

Visualforce Page:

<apex:page controller="sendsmscls" >
<apex:Form >
    <apex:CommandButton value="Send SMS" action="{!sendsms}"/>
</apex:Form>
</apex:page>

Apex Class:

global class sendsmscls{

    webservice static void sendsms(){

        /*String address='Ameerpet, Hyderabad';
           HttpRequest req = new HttpRequest();
         //Setting Endpoint URL to HTTP request
            req.setEndpoint('http://api.clickatell.com/http/sendmsg?user=praveen418&password=praveen@123&api_id=3363654&to=+919032011248&text=Hello');
          //set the request type
            req.setMethod('GET');
          //HTTP object creation
            Http http = new Http();
          //Sending the Req throgh HTTPResponse object.
            HTTPResponse res = http.send(req);

        /*HTTPRequest req1=new HTTPRequest();
            req1.setEndpoint('http://s2.freesmsapi.com/messages/send?skey=4e2bb3add94d00160d1288946658956f&message=Good Morning&recipient=9440490498');
            req1.setMethod('GET');
            HTTP h1=new HTTP();
            httpresponse res1 = h.send(req1);
            System.debug('--------------'+res1.getBody()); */
                                                             }
                                             }

Note: we need to add http://api.clickatell.com to Remotesite Settings.
Note: we need to add http://s2.freesmsapi.com to Remotesite Settings.

Below Apex class is sends sms to contact persons whose case closed in contact object 


global with sharing class sendsmscls {
@future(callout=true)
webservice static void sendsms(String caseid){

Case c =[Select contactid, casenumber from Case where id=:caseid];
if(c.contactid != null){
Contact con =[Select id, mobilephone from Contact where id=:c.contactid ];
if(con.mobilephone != null){

String txt='Your case '+c.casenumber+' has been closed';
HttpRequest req = new HttpRequest();
//Setting Endpoint URL to HTTP request
req.setEndpoint('http://api.clickatell.com/http/sendmsg?user=praveen418&password=praveen@123&api_id=3363654&to='+con.mobilephone+'&text='+txt);
//set the request type
req.setMethod('GET');
//HTTP object creation
  Http http = new Http();
//Sending the Req throgh HTTPResponse object.
HTTPResponse res = http.send(req);

System.debug('---------->'+res);
}
}

}
}

No comments:

Post a Comment