简体   繁体   English

实现从Salesforce到Twilio之间的批量消息传递,达到Salesforce API的限制

[英]Implementing bulk-messaging from Salesforce to/from Twilio, hitting Salesforce API limits

I am building an integration between Salesforce and Twilio that sends/receives SMS using TwilioForce REST API. 我正在建立Salesforce和Twilio之间的集成,该集成使用TwilioForce REST API发送/接收SMS。 The main issue is getting around the 10-call API limit from Salesforce, as well as the prohibition on HTTP call outs from a trigger. 主要问题是解决Salesforce的10调用API限制,以及禁止触发器触发HTTP调用。

I am basing the design on Dan Appleman's Asynchronous Request processes, but in either Batch mode or RequestAsync(), ASync(), Sync(), repeat... I'm still hitting the limits. 我将设计基于Dan Appleman的“异步请求”流程,但是在批处理模式或RequestAsync(),ASync(),Sync()中,重复...我仍在达到极限。

I'd like to know how other developers have done this successfully; 我想知道其他开发人员如何成功做到这一点; the integrations have been there for a while, but the examples are few and far between. 集成已经存在了一段时间,但是示例很少而且相去甚远。

Are you sending unique messages for each record that has been updated? 您是否为每个已更新的记录发送唯一消息? If not, then why not send one message to multiple recipients to save on your API limits? 如果不是,那么为什么不发送一条消息给多个收件人以节省您的API限制?

Unfortunately, if you do actually need to send more than 10 unique messages there is no way to send messages in bulk with the Twilio API, you could instead write a simple application that runs on Heroku or some other application platform that you can call out to that will handle the SMS functionality for you. 不幸的是,如果您确实确实需要发送10条以上的唯一消息,则无法使用Twilio API批量发送消息,您可以编写一个运行在Heroku或其他应用程序平台上的简单应用程序,它将为您处理SMS功能。

I have it working now using the following structure (I apologize for the formatting - it's mostly pseudocode): 我现在使用以下结构进行工作(我很抱歉格式化-它主要是伪代码):

ASyncRequest object: AsyncType (picklist: 'SMS to Twilio' is it for now), Params (long text area: comma-separated list of Ids) ASyncRequest对象:AsyncType(选择列表:暂时是“从SMS到Twilio”),Params(长文本区域:以逗号分隔的ID列表)

Message object: To (phone), From (phone), Message (text), Sent (boolean), smsId (string), Error (text) 消息对象:收件人(电话),发件人(电话),消息(文本),发送(布尔),smsId(字符串),错误(文本)

Message trigger: passes trigger details to CreateAsyncRequests() method. 消息触发器:将触发器详细信息传递给CreateAsyncRequests()方法。

CreateAsyncRequests: evaluate each new/updated Message__c; CreateAsyncRequests:评估每个新的/更新的Message__c; if Sent == false for any messages, we create an AsyncRequest, type=SMS to Twilio, Params += ',' + message.Id. 如果对于任何消息,Sent == false,我们将创建一个AsyncRequest,将TMS = TMS发送到Twilio,Params + =','+ message.Id。

// Create a list to be inserted after all the Messages have been processed List requests = new List(); //创建一个要在所有消息处理完毕后插入的列表。list request = new List();

Once we reach 5 message.Ids in a single AsyncRequest.Params list, add it to requests. 一旦在单个AsyncRequest.Params列表中达到5个message.Ids,将其添加到请求中。 If all the messages have been processed and there's a request with < 5 Ids in Params, add it to requests as well. 如果所有消息都已处理,并且在Params中有一个<5 Ids的请求,请将其也添加到请求中。

If requests.size() > 0 {
  insert requests;
  AsyncProcessor.StartBatch();
}

AsyncProcessor implements .Batchable and .AllowsCallouts, and queries ASyncRequest__c for any requests that need to be processed, which in this case will be our Messages list. AsyncProcessor实现.Batchable和.AllowsCallouts,并向ASyncRequest__c查询需要处理的任何请求,在这种情况下,这将是我们的消息列表。

The execute() method takes the list of ASyncRequests, splits each Params value into its component Message Ids, and then queries the Message object for those particular Messages. execute()方法获取ASyncRequest的列表,将每个Params值拆分为其组件Message Ids,然后在Message对象中查询那些特定的Message。

StartBatch() calls execute() with 1 record at a time, so that each execute() process will still contain fewer than the maximum 10 callouts. StartBatch()一次调用一次具有1条记录的execute(),因此每个execute()进程所包含的数量仍少于最大10个标注。

Each Message is processed in a try/catch block that calls SendMessage(), sets Message.smsId = Twilio.smsId and sets Message.Sent = true. 在调用SendMessage()的try / catch块中处理每个消息,设置Message.smsId = Twilio.smsId并设置Message.Sent = true。

If no smsId is returned, then the message was not sent, and I set a boolean bSidIsNull = true indicating that (at least) one message was not sent. 如果未返回任何smsId,则说明未发送消息,并且我设置了一个布尔值bSidIsNull = true,指示(至少)未发送一条消息。

** If any message failed, no smsIds are returned EVEN FOR MESSAGES THAT WERE SUCCESSFUL ** **如果任何消息失败,则即使成功发送消息也不会返回smsIds **

After each batch of messages is processed, I check bSidIsNull; 处理完每一批消息后,我检查bSidIsNull; if true, then I go back over the list of messages and put any that do not have an smsId into a map indexed by the Twilio number I'm trying to send them From. 如果为true,那么我将返回邮件列表,并将没有smsId的邮件放入由我试图从中发送的Twilio编号索引的映射中。

Since I limited each ASyncRequest to 5 messages, I still have the use of a callout to retrieve all of the messages sent from that Twilio.From number for the current date, using 由于我将每个ASyncRequest限制为5条消息,因此我仍然可以使用标注来检索从该Twilio发送的所有消息。

client.getAccount().getMessages('From' => fromNumber, 'DateSent' => currentDate)

Then I can update the Message.smsIds for all of the messages that were successful, and add an error message to Message.Error_on_Send__c for any that failed. 然后,我可以为所有成功的消息更新Message.smsIds,并为所有失败的消息添加错误消息到Message.Error_on_Send__c。

Most of the international Texting API (twilio , nexmo... ) are not suitable for salesforce as per the limitations. 根据限制,大多数国际Texting API(twilio,nexmo ...)都不适合Salesforce。

Utilizing 1 API call for 1 SMS is not a good option. 将1个API调用用于1条SMS并不是一个好的选择。 It is not suggested way by using batch job or future methods to handle these calls. 建议不要使用批处理作业或将来的方法来处理这些调用。

Dynamic message creation is also some time challenging when you are referring cross object fields. 当引用跨对象字段时,动态消息创建也需要一些时间。

We have created an App where it can process up to 40K SMS in "1" API call and it is very easy to manage SMS template like Email Template. 我们创建了一个应用程序,在“ 1” API调用中它可以处理多达40K条短信,并且非常容易管理电子邮件模板之类的短信模板。

You can also setup SMS from workflow or Process builder like Email. 您也可以从工作流或“电子邮件”之类的流程构建器设置SMS。

You can use ValueText SMS Api to send bulk SMS from any platform. 您可以使用ValueText SMS Api从任何平台发送批量SMS。

ValueText SMS App : https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000EFoedUAD ValueText SMS应用程序: https ://appexchange.salesforce.com/appxListingDetail?listingId = a0N3A00000EFoedUAD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM