-2

I am currently building a Slack Bot using Node JS to consume my Rails API.

I'd love to give Slack users who use the bot the option to view detailed help when they put in certain keywords.

Currently, my code looks like this:

        var msg = "To edit a task, specify its alias and include the edit flag (--e). \r\n \
        Then every other option you provide would replace existing ones. \r\n \
 \r\n \
        For example, \r\n \
 \r\n \
        ```task send freckle reminders to me --e --t 10:00am --a freckle```  \r\n \
 \r\n \
        changes an existing task with alias 'freckle' to:  \r\n \
        ``` \r\n \
        task: send freckle reminders to me \r\n \
        time: 10:00am
        ```
        " + dmAuthor;

You may choose to ignore the \r\n part, since Slack wouldn't give me any other option to break text.

My main issue is with the javascript backslash escape as my best resort (at least that's what this blog claims.

Is there any better way I could handle large texts (something similar to pHp's HEREDOC block) ?

Cent
  • 871
  • 7
  • 16
  • 1
    I'd suggest putting all the text in some external storage, either a file per message, a set of JSON files that contain a bunch of messages, a database or something like that. Then, you code is your code and the data comes from an external source that it more easily maintained as an external data source rather than as code. – jfriend00 Aug 15 '15 at 23:38
  • @jfriend00 true, the API it consumes is mine. I could easily make allowances for help. But this information would likely change relatively little. Plus performance wise, I feel static variables would be served faster than the bot having to perform a file read or data request. – Cent Aug 15 '15 at 23:50
  • @Gothdo .. Its not a duplicate. I'm trying to avoid doing exactly what the answer to that question advised. – Cent Aug 15 '15 at 23:50
  • There is no pretty way to put multi-line text into Javascript. There just isn't. You either have to use continuation characters or `+` operators or join multiple strings. It's just ugly. So, if you have lots of it, I wouldn't do it. I'd put it all in one or more external files. If you want the data all instantly available, then just read it once upon server startup into memory. – jfriend00 Aug 15 '15 at 23:53
  • Oh. good point: `read it once upon server startup into memory`. If the data grows too big (over four paragraphs), I'd do exactly that. – Cent Aug 16 '15 at 00:03

2 Answers2

1

two inline options:

joining an array

var msg = ['To edit a task, specify its alias and include the edit flag (--e).',
           'Then every other option you provide would replace existing ones.',
           'For example,',
           '```task send freckle reminders to me --e --t 10:00am --a freckle```',
           'changes an existing task with alias 'freckle' to:',
           '```',
           'task: send freckle reminders to me',
           'time: 10:00am',
           '```'].join('\r\n');

concatenation (not as nice on newlines, there are none in the next block):

var msgAlt = 'To edit a task, specify its alias and include the edit flag (--e).' +
             'Then every other option you provide would replace existing ones.' +
             'For example,' +
             '```task send freckle reminders to me --e --t 10:00am --a freckle```' +
             'changes an existing task with alias 'freckle' to:' +
             '```' +
             'task: send freckle reminders to me' +
             'time: 10:00am' +
             '```';

non-inline:

save it in an external file or database and read it in when you need it.

user3276552
  • 1,074
  • 12
  • 15
  • Oh.. The Array.join() technique looks really cool. It allows for greater readability. Since I'm coming from a Ruby background, code readability is emphasized. I'd try it right now, then mark as answer once it works satisfactorily. Thanks. – Cent Aug 15 '15 at 23:46
1

One option to test, as Slack doesn't really use Node, but rather, uses the engine behind the Atom editor, which, itself uses iojs, rather than Node...

In ES6 (also available today in Chrome, and thus, iojs), backticks are valid string delimiters.

var myString = `this is my string`;

Unlike prime and double-prime ("single quote" and "quote"), newlines are entirely valid:

var hereADocThereADoc = `
This is whitespace-significant content.
 Anything in this realm follows the rules of the whitespace provided.
`;

Another bonus: unlike the other two types of string, it also supports value interpolation using ${ } as a wrapper around an expression, which may be a JS variable, method call, or any other valid expression

var printout = `total price: ${ getCurrencySymbol() }${ items.reduce(sum, 0).toFixed(2) }`;

That's far from the best code ever, but my lazy example can't hide how useful all of that is.

It also has the ability to define a tag, a function name which references a function that will be called with all of the raw values and all of the interpolated values, when it comes time to evaluate that string.

If we were talking client-side, I'd typically do a join, or use a transpiler like Babel. But this isn't browser JS.

Give it a try and see if it works for you.

Norguard
  • 26,167
  • 5
  • 41
  • 49
  • Oh.. wow. Thanks for the tips. I'm currently doing joins. I greatly appreciate the interpolation pointers though. They're already coming in handy – Cent Aug 16 '15 at 21:48