How to add an external module ( ex : system-sleep ) to package.json?

Hi
I need for a custom action to use “system-sleep”.
I understand that I need to add the package “to my module”, but do not understand how to do it .
I was pointed to this : https://botpress.io/docs/create-module#register-actions for the action ( fine I think )
and foudn this to add package : https://botpress.io/docs/create-module#register-actions
but it doesn’t tell me so much at the end , or I don’t get it which is an option as well …
I have just a custom action into which I want to use system-sleep.
Anyone would have some insights or better, an example ?
Thanks in advance !

How about:

yarn add system-sleep

in your module.

P.s: I guess the new document is here

Please provide your complete module, if you still have an issue.

hello @asashour, thanks a lot
in fact, i just want to create an action, i don’t really need a full module.

for sleep, I was using npm which was not doing the job obvisouly …

I tried " yarn add system-sleep " and it worked I think, I don’t have anymore warning about sleep not existing …
but the bot is “stalled”.
I created a basic new bot for troubleshooting :
image

I created a new custom action ( mywait.js ) which contains :

var sleep = require(‘system-sleep’);
for (y = 0; y < 10; y++) {
console.log(y);
sleep(1000);
}

and so , in the logs I have :

Could not perform predict predict data TypeError: Cannot read property ‘length’ of undefined
at predictContext (/opt/botpress/modules/.cache/module__69ccc92ce12a1dd15eaa455512863282771df01154426f2a9bd681d75b588cbd/dist/backend/engine2/predict-pipeline.js:101:26)
at Predict (/opt/botpress/modules/.cache/module__69ccc92ce12a1dd15eaa455512863282771df01154426f2a9bd681d75b588cbd/dist/backend/engine2/predict-pipeline.js:351:24)
13:11:32.014 bp:dialog (test) [R-0dsgjgjalgd54kybNov] init new context { currentNode: ‘entry’, currentFlow: ‘main.flow.json’ }{ botId: ‘test’ }, +0ms
13:11:32.015 bp:dialog (test) [R-0dsgjgjalgd54kybNov] eval transition “always” to [node-1928]{ botId: ‘test’ }, +0ms
13:11:32.016 bp:dialog (test) [R-0dsgjgjalgd54kybNov] transit (main.flow.json) [entry] -> [node-1928]{ botId: ‘test’ }, +4ms
13:11:32.017 bp:dialog (test) [R-0dsgjgjalgd54kybNov] execute action “builtin/mywait”{ botId: ‘test’ }, +2ms
13:11:32.037 bp:actions (test) run action{ actionName: ‘builtin/mywait’,
incomingEvent:
IOEvent {
type: ‘text’,
channel: ‘web’,
direction: ‘incoming’,
payload: { type: ‘text’, text: ‘hi’ },
target: ‘R-0dsgjgjalgd54kybNov’,
botId: ‘test’,
createdOn: 2020-01-13T13:11:31.797Z,
threadId: ‘102’,
id: ‘157892109179701220’,
preview: ‘hi’,
flags: {},
state:
{ __stacktrace: [Array],
user: [Object],
context: [Object],
session: [Object],
temp: {},
bot: undefined },
suggestions: [],
credentials: undefined,
nlu:
{ entities: [],
language: ‘n/a’,
detectedLanguage: ‘n/a’,
ambiguous: false,
slots: {},
intent: [Object],
intents: [],
errored: true,
includedContexts: [Array],
ms: 0 },
decision:
{ decision: [Object],
confidence: 1,
payloads: [],
source: ‘decisionEngine’,
sourceDetails: ‘execute default flow’ } },
actionArgs: {} }, { botId: ‘test’ }, +0ms
> 13:11:47.267 DialogJanitor Skipping the current run, previous operation still running
> 13:11:57.267 DialogJanitor Skipping the current run, previous operation still running
> 13:12:07.267 DialogJanitor Skipping the current run, previous operation still running
> 13:12:17.266 DialogJanitor Skipping the current run, previous operation still running
> 13:12:17.268 LogsJanitor Skipping the current run, previous operation still running
> 13:12:27.267 DialogJanitor Skipping the current run, previous operation still running

I am able to reproduce your issue.

For some reason, the system-sleep doesn’t work.

You can achieve the exact needed result, without any dependencies, using:

await new Promise(resolve => setTimeout(resolve, 10000))

Enjoy!

hi @asashour
it doesn’t like await function …
I have in logs :

14:50:54.680 ActionService An error occurred while executing the action "builtin/wait_perso [SyntaxError, await is only valid in async function]
STACK TRACE
vm.js:38
> await new Promise(resolve => setTimeout(resolve, 10000));
> ^^^^^

SyntaxError: await is only valid in async function
at new Script (vm.js:75:7)
at VMScript.compile (/snapshot/build-linux/node_modules/vm2/lib/main.js:80:20)

is there something strange with my config ?

Below is the full code, please open your action from the “Code Editor”, you would easily find any errors.

Please see the

const myAction = async (name, value) => {

Yours,

async function action(bp: typeof sdk, event: sdk.IO.IncomingEvent, args: any, { user, temp, session } = event.state) {
  /** Your code starts below */

  /**
   * Small description of your action
   * @title The title displayed in the flow editor
   * @category Custom
   * @author Your_Name
   * @param {string} name - An example string variable
   * @param {any} value - Another Example value
   */
  const myAction = async (name, value) => {
    await new Promise(resolve => setTimeout(resolve, 10000))
  }

  return myAction(args.name, args.value)

  /** Your code ends here */
}

Thanks a lot :smiley: … it works
I 'm using vi for editing custom actions …