How to use velocity for SMS templates

Luis Rico :

I am working on a Java service that needs to send SMS through Amazon SNS.

I am using Velocity templates to generate personalized emails, and thought about using it for SMS as well.

But I don't think it is the right approach because the AWS SDK method for sending an SMS takes the message as a string. This would force me to generate a file and then read it to get the contents as a string.

The only alternative I can think of is storing the template as TINYTEXT (SMS size limit is 140 bytes) in the database, and use String.replaceAll() instead of velocity.

But I wanted to know if there is a better way to do it or if using velocity would hurt performance that much.

user7294900 :

You can use velocity without generating a file until VelocityEngine.evaluate

renders the input string using the context into the output writer. To be used when a template is dynamically constructed, or want to use Velocity as a token replacer.

Example:

VelocityContext context = new VelocityContext();
context.put("param", paramMap);
context.put("placeList", placeList);
StringWriter writer = new StringWriter();
ve.evaluate(context, writer, "", template);
return writer.toString();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=134959&siteId=1