## <span id="brightpacks">BrightPacks</a>

BrightPacks are a combination of the out-of-the-box Bright Stacks and any Bright bots you build to add additional services to your application.

## <span id="brightstacks">BrightStacks</a>

Stacks can be accessed anywhere in BrightBot code by using the globally defined Stack object.  You can also make calls to a stack using the RESTful endpoint (http://appname.bwapps.io/api/Stack)

### <span id="email">Email Stack</a>

**Global Object:** *Stacks.EmailStack*

**REST Endpoint:** *appname.bwapps.io/api/Email

<dl>
<dt><a href="#send">send(msg)</a></dt>
<dd><p>Send an email message.</p>
</dd>
<dt><a href="#sendTemplated">sendTemplated(templateId, msg)</a></dt>
<dd><p>Send an email message using a predefined template.</p>
</dd>
</dl>

<span id="send"></a>

#### send(msg)
Send an email message.

| Param | Type | Description |
| --- | --- | --- |
| msg | <code>Object</code> | The message and content to send. |
| msg.to | <code>Array.&lt;string&gt;</code> | The message receipients to send email to. |
| msg.from | <code>string</code> | The email address of the sender. |
| msg.subject | <code>string</code> | The subject line of the email. |
| msg.text | <code>string</code> | The text only version of the email. |
| msg.html | <code>string</code> | The html version of the email. |
| msg.cc | <code>Array.&lt;string&gt;</code> | The message CC receipients to send the email to. |
| msg.bcc | <code>Array.&lt;string&gt;</code> | The message BCC receipients to send email to. |
| msg.replyto | <code>string</code> | The reply to address. Only supply if different than from address. |

<span id="sendTemplated"></a>

#### sendTemplated(templateId, msg)
Send an email message using a predefined template.

| Param | Type | Description |
| --- | --- | --- |
| templateId | <code>string</code> | The identifier of the template to use when sending this email. |
| msg | <code>Object</code> | The message and content to send. |
| msg.to | <code>Array.&lt;string&gt;</code> | The message receipients to send email to. |
| msg.from | <code>string</code> | The email address of the sender. |
| msg.subject | <code>string</code> | The subject line of the email. |
| msg.text | <code>string</code> | The text only version of the email. |
| msg.html | <code>string</code> | The html version of the email. |
| msg.cc | <code>Array.&lt;string&gt;</code> | The message CC receipients to send the email to. |
| msg.bcc | <code>Array.&lt;string&gt;</code> | The message BCC receipients to send email to. |
| msg.replyto | <code>string</code> | The reply to address. Only supply if different than from address. |

### <span id="object-storage">Object Storage Stack</a>

**Global Object:** *Stacks.StorageStack*

**REST Endpoint:** *appname.bwapps.io/api/Storage

<dl>
<dt><a href="#createContainer">createContainer(name, cb)</a></dt>
<dd><p>Create a storage container.  This is analogous to a folder on your local file system or a bucket if you are familiar with the Amazon S3 storage.</p>
</dd>
<dt><a href="#deleteContainer">deleteContainer(name, cb)</a></dt>
<dd><p>Delete a storage container.</p>
</dd>
<dt><a href="#createObject">createObject(container, name, content, cb)</a></dt>
<dd><p>Create an object and save it to a storage container.</p>
</dd>
<dt><a href="#getObject">getObject(container, name, cb)</a></dt>
<dd><p>Fetch an object from a storage container. This method returns the content and metadata for the object.</p>
</dd>
<dt><a href="#deleteObject">deleteObject(container, name, cb)</a></dt>
<dd><p>Delete an object that has been previously saved to a storage container.</p>
</dd>
</dl>

<span id="createContainer"></a>

#### createContainer(name, cb)
Create a storage container.  This is analogous to a folder on your local file system or a bucket if you are familiar with the Amazon S3 storage.

| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | The name of the container. |
| cb | <code>function</code> |  |

<span id="deleteContainer"></a>

#### deleteContainer(name, cb)
Delete a storage container.

| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | The name of the container. |
| cb | <code>function</code> |  |

<span id="createObject"></a>

#### createObject(container, name, content, cb)
Create an object and save it to a storage container.

| Param | Type | Description |
| --- | --- | --- |
| container | <code>string</code> | The name of the container. |
| name | <code>string</code> | The name of the object. |
| content | <code>string</code> &#124; <code>buffer</code> &#124; <code>stream</code> | The content of the object |
| cb | <code>function</code> |  |

<span id="getObject"></a>

#### getObject(container, name, cb)
Fetch an object from a storage container. This method returns the content and metadata for the object.

| Param | Type | Description |
| --- | --- | --- |
| container | <code>string</code> | The container name. |
| name | <code>string</code> | The name of the object to fetch. |
| cb | <code>function</code> |  |

<span id="deleteObject"></a>

#### deleteObject(container, name, cb)
Delete an object that has been previously saved to a storage container.

| Param | Type | Description |
| --- | --- | --- |
| container | <code>string</code> | The container name. |
| name | <code>string</code> | The object name. |
| cb | <code>function</code> |  |

## <span id="brightbots">BrightBots</a>
The microservices you use to control your application logic. They can be simple services that respond to an event or a RESTful route.

Follow the template and example below to setup RESTful bots in the root of your project. (Event Bot support coming soon)

### <span id="restful-bots">Restful Bots</a>
----------------
**Basic RESTful Bot Template**

```javascript
'use strict';

const RestBot = require('bw-brightbot').RestBot;

class NewExampleBot extends RestBot {

    constructor() {
        super()
    }

    get trigger() {
        return 'get /newpath';
    }

    action(context) {
        return { someData: asObject };
    }
}

module.exports = NewExampleBot;
```

**RESTful Bot Example: Query Twitter API to get all users tweets**

```javascript
'use strict';

const RestBot = require('bw-brightbot').RestBot;

class TwitterBot extends RestBot {

    constructor() {
        super()
    }

    authenticate() {

      let consumer_key = 'CONSUMER_KEY';
      let consumer_secret = 'CONSUMER_SECRET';
      let consumerStr = `${consumer_key}:${consumer_secret}`;
      let encodedStr = window.btoa(consumerStr);

      let options = {
        method: 'POST',
        uri: 'https://api.twitter.com/oauth2/token',
        body: 'grant_type=client_credentials',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
          'Authorization': `Basic ${encodedStr}`
        },
        json: true
      };
      return request(options)
        .then((response)=> response.access_token);
    }

    getTweets(token, id) {
      let options = {
        method: 'GET',
        uri: `https://api.twitter.com/user_timeline.json?screen_name=${id}`,
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
          'Authorization': `Bearer ${token}`
        },
        json: true
      };
      return request(options)
        .then((response)=> {
          return { 'tweets': response };
        });
    }

    get trigger() {
        return 'get /tweets/:id';
    }

    action(context) {
        return this.authenticate().then((token)=>{
          return this.getTweets(token, context.id);
        });
    }
}

module.exports = TwitterBot;
```

## <span id="contact-and-support">Contact & Support</a>
We're always trying to improve our product. Get in touch with us by email: <support@brightwork.io>