Salesforce 发送短信信息

来源:互联网 发布:中国象棋哪个软件最好 编辑:程序博客网 时间:2024/05/17 05:19

public class SMS {
private String url;
private String url1;
private String url2;
private String result;

public SMS(){    url='';    url1='';    url2='';    result=''; }//发送一条短信,接收一个电话号码和短信内容,返回一个请求结果状态public String sendSMS (String content,String phone,String exId) {    url2 = '&ac='+Label.SMSAccount        +'&authkey='+ Label.SMSPassWord         +'&cgid=16&c=' + EncodingUtil.urlEncode(content, 'utf8')        +'&m='+phone;    url1 = 'http://192.168.0.20:8012/OpenPlatform/OpenApi?action=sendOnce';    //构造短信接口url格式    url = url1 + url2;     system.debug('url'+url);    //发送http请求,需要在远程站点设置用设置访问的网址url,否则请求会失败               HttpRequest req = new HttpRequest();    req.setEndpoint(url);    req.setMethod('GET');     req.setTimeout(12000);    Http http = new Http();    HttpResponse res = http.send(req);    System.debug(res.getStatus()+'==============sms result========' +'|'+res.getStatusCode());    System.debug('==============sms result2========' +'|'+res.getBody());    //测试短信接口返回的状态,100表示已经发送(请求的格式正确,成功连接到短信接口),但是并不代表短信发送到对方手机上    //当手机号码为连续的任何数字都会成功并返回100状态    XmlStreamReader xsr = new XmlStreamReader(res.getBody());        SMSInfo__c info =[SELECT id,Status__c,msgId__c FROM SMSInfo__c WHERE TimeNum__c=:exId limit 1];    xsr.next();    while(xsr.hasNext()){        if(xsr.isStartElement()){            system.debug('name:'+xsr.getLocalName());            if(xsr.getLocalName()=='xml'){                for(integer i=0;i<xsr.getAttributeCount();i++){                    if(xsr.getAttributeLocalName(i)=='result'){                        info.Status__c = xsr.getAttributeValueAt(i);                    }                }                }else if(xsr.getLocalName()=='Item'){                for(integer i=0;i<xsr.getAttributeCount();i++){                    if(xsr.getAttributeLocalName(i)=='msgid'){                        info.msgId__c = xsr.getAttributeValueAt(i);                    }                }            }         }        xsr.next();    }    update info;    return res.getStatus();}public static void doFutureSend(String phone,String content){    SMSInfo__c info = new SMSInfo__c();    info.phone__c = phone;    info.content__c = content;    info.Status__c = '2';//未发送    info.TimeNum__c = String.valueOf(DateTime.now().getTime());    insert info;    sendMethod(phone,content,info.TimeNum__c);}//出差申请与拜访记录插入 微信  数据public static void doInsert(String content, List<User> leaders, String type){    List<SMSInfo__c> smsList = new List<SMSInfo__c>();    DateTime now = System.now();         Datetime lastDateTime = now.addDays(-2);    List<SMSInfo__c> infoList = [SELECT Id, content__c, phone__c FROM SMSInfo__c where (addDate__c>: lastDateTime and  addDate__c<:now)  and type__c =: type ];    Map<String, SMSInfo__c> infoMap = new Map<String, SMSInfo__c>();    for(SMSInfo__c info : infoList){        infoMap.put(info.content__c, info);    }            Integer i = 0;      if(!infoMap.containsKey(content)){        for(User leader : leaders){                        SMSInfo__c info = new SMSInfo__c();            info.weChatAgentId__c= Label.weChatAgentId;            info.weChatNumber__c= leader.weChatNumber__c;            info.phone__c = leader.phone;            info.content__c = content;            info.Status__c = '2';//未发送            info.type__c = type;            info.addDate__c = now;             info.TimeNum__c = String.valueOf(DateTime.now().getTime() + i++);            smsList.add(info);                }         }    insert smsList;        }//出差申请与拜访记录插入  短信  数据public static void doInsert2(String content, List<User> leaders, String type){    List<SMSInfo__c> smsList = new List<SMSInfo__c>();    DateTime now = System.now();         Datetime lastDateTime = now.addDays(-2);    List<SMSInfo__c> infoList = [SELECT Id, content__c, phone__c FROM SMSInfo__c where (addDate__c>: lastDateTime and  addDate__c<:now)  and type__c =: type ];    Map<String, SMSInfo__c> infoMap = new Map<String, SMSInfo__c>();    for(SMSInfo__c info : infoList){        infoMap.put(info.content__c, info);    }            Integer i = 0;      if(!infoMap.containsKey(content)){        for(User leader : leaders){                        SMSInfo__c info = new SMSInfo__c();            info.phone__c = leader.phone;            info.content__c = content;            info.Status__c = '2';//未发送            info.type__c = type;            info.addDate__c = now;             info.TimeNum__c = String.valueOf(DateTime.now().getTime() + i++);            smsList.add(info);                }         }    insert smsList;        }@future(callout=true)public static void sendMethod(String phoneNum,String content,String exId){    SMS s= new SMS();    String status ='';    status = s.sendSMS(content, phoneNum, exId); }    //验证电话号码是否正确     public boolean isPhoneNumber(String str) {        boolean flag  = false;            Pattern p = Pattern.compile('^[1][3,4,5,8][0-9]{9}$'); // 验证手机号      Matcher m = p.matcher(str);        return m.matches(); }

}

0 0