Salesforce 发送短信信息
2016-07-04 11:00
381 查看
public class SMS {
private String url;
private String url1;
private String url2;
private String result;
}
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(); }
}
相关文章推荐
- 基于JavaScript短信验证码如何实现
- Android接收和发送短信处理
- Android开发中实现发送短信的小程序示例
- 基于JavaScript实现手机短信按钮倒计时(超简单)
- Android短信操作常见协议和常用代码
- 仿iphone中短信以及通话记录的时间显示
- php短信接口代码
- 手机短信轰炸(图)
- SSH框架网上商城项目第26战之订单支付后发送短信提醒
- Android编程之SMS读取短信并保存到SQLite的方法
- Android实现短信加密功能(发送加密短信、解密本地短信)
- Android创建简单发送和接收短信应用
- 浅析Android手机卫士接收短信指令执行相应操作
- Android实现将已发送的短信写入短信数据库的方法
- Android短信发送器实现方法
- Android实现短信发送功能
- Android中简单的电话管理与短信管理App编写实例
- Android编程实现读取手机联系人、拨号、发送短信及长按菜单操作方法实例小结
- Android获取短信验证码的实现方法