Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
add studio.getById
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Brown committed Feb 15, 2018
1 parent ea226b5 commit 7b9bfe5
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

[Want to contribute? Read our guide!](https://github.com/howdyai/botkit/blob/master/CONTRIBUTING.md)

# 0.6.11

* For Botkit Studio users, added `controller.studio.getById()` for loading scripts
by their unique ID rather than by name or trigger. [Docs here](docs/readme-studio.md#controller-studio-getbyid)

# 0.6.10

* Add support for Cisco Jabber. Thanks to @qiongfangzhang and @panx981389 and their team at Cisco for the contribution!
Expand Down
25 changes: 25 additions & 0 deletions docs/readme-studio.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,31 @@ controller.studio.get(bot, 'hello', message.user, message.channel).then(function
```


### controller.studio.getById()
| Argument | Description
|--- |---
| bot | A bot instance
| id | The id of a script defined in Botkit Studio
| user | the user id of the user having the conversation
| channel | the channel id where the conversation is occurring

`controller.studio.getById()` does the same thing as `get()`, but uses the scripts unique ID rather than it's name.

While developers may still tap into the conversation as it is conducted using the [before](#controllerstudiobefore), [after](#controllerstudioafter), and [validate](#controllerstudiovalidate) hooks, it must first be activated using `convo.activate()` in the results of the promise returned by the function.

This enables developers to add template variables to the conversation object before it sends its first message. Read about [using variables in messages](readme.md#using-variable-tokens-and-templates-in-conversation-threads)

```javascript
controller.studio.getById(bot, '5a7381d85d49c200142ed7bf', message.user, message.channel).then(function(convo) {
convo.setVar('date', new Date()); // available in message text as {{vars.date}}
convo.setVar('news', 'This is a news item!'); // ailable as {{vars.news}}

// crucial! call convo.activate to set it in motion
convo.activate();
});
```


### controller.studio.runTrigger()
| Argument | Description
|--- |---
Expand Down
62 changes: 62 additions & 0 deletions lib/Studio.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ module.exports = function(controller) {
return sdk.createScript(trigger, text);
};

// load a script from the pro service
controller.studio.getScriptById = function(bot, id, user) {

var userHash = md5(user);
var sdk = new SDK(genConfig(bot));
return sdk.getScriptById(id, user);
};

// load a script from the pro service
controller.studio.getScript = function(bot, text, user) {

Expand Down Expand Up @@ -221,6 +229,60 @@ module.exports = function(controller) {
});
};

/* Fetch a script from Botkit Studio by id, but do not execute it.
* returns a promise that resolves when the conversation is loaded
* but developer still needs to call convo.activate() to put it in motion */
controller.studio.getById = function(bot, id, user, channel, original_message) {
var context = {
id: id,
user: user,
channel: channel,
raw_message: original_message ? original_message.raw_message : null,
original_message: original_message || null
};
return new Promise(function(resolve, reject) {
controller.studio.getScriptById(bot, id, user).then(function(command) {
console.log('RESULTS', command);
if (command !== {} && command.id) {
controller.trigger('command_triggered', [bot, context, command]);

// make the script source information from Botkit Studio available to Botkit's convo object
context.script_name = command.command;
context.script_id = command._id;

controller.studio.compileScript(
bot,
context,
command
).then(function(convo) {
convo.on('end', function(convo) {
runHooks(
after_hooks[command.command] ? after_hooks[command.command].slice() : [],
convo,
function(convo) {
controller.trigger('remote_command_end', [bot, context, command, convo]);
}
);
});
runHooks(
before_hooks[command.command] ? before_hooks[command.command].slice() : [],
convo,
function(convo) {
resolve(convo);
}
);
}).catch(function(err) {
reject(err);
});
} else {
reject('Script not found');
}
}).catch(function(err) {
reject(err);
});

});
};

controller.studio.runTrigger = function(bot, input_text, user, channel, original_message) {
var context = {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "botkit",
"version": "0.6.10",
"version": "0.6.11",
"description": "Building blocks for Building Bots",
"main": "lib/Botkit.js",
"types": "lib/Botkit.d.ts",
Expand All @@ -9,7 +9,7 @@
"back": "^1.0.1",
"body-parser": "^1.17.1",
"botbuilder": "^3.8.4",
"botkit-studio-sdk": "^1.0.7",
"botkit-studio-sdk": "^1.0.8",
"ciscospark": "^1.26.0",
"clone": "2.1.1",
"command-line-args": "^4.0.2",
Expand Down

0 comments on commit 7b9bfe5

Please sign in to comment.