Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added getTimeSinceLastConversation() and getTotalNumberOfConversations()
- Loading branch information
1 parent
43d725c
commit b068be3
Showing
7 changed files
with
129 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const USER_TAG_CONVO_COUNT = 'BUILTIN_CONVO_COUNT' | ||
export const USER_TAG_CONVO_LAST = 'BUILTIN_CONVO_LAST' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * as contentElements from './elements' | ||
export contentRenderers from './renderers' | ||
export * as actions from './actions' | ||
export setup from './setup' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { USER_TAG_CONVO_COUNT, USER_TAG_CONVO_LAST } from './common' | ||
|
||
/** | ||
* This method should be called on bot boot in order | ||
* for the different actions and renderers to work properly. | ||
* | ||
* This method will setup and inject different middleware. | ||
* | ||
* @param {Botpress} bp The global Botpress instance | ||
*/ | ||
export default function(bp) { | ||
// Tracks new user conversations | ||
// Used by the following actions: | ||
// - Get total number of conversations | ||
bp.dialogEngine.onBeforeCreated(async (ctx, next) => { | ||
const { stateId } = ctx | ||
|
||
if (!stateId.includes(':')) { | ||
// Unknown platform / can't extract userId | ||
return next() | ||
} | ||
|
||
const convoCount = await bp.users.getTag(stateId, USER_TAG_CONVO_COUNT) | ||
await bp.users.tag(stateId, USER_TAG_CONVO_COUNT, parseInt(convoCount || 0) + 1) | ||
|
||
next() | ||
}) | ||
|
||
// Tracks conversation endings | ||
// Used by the following actions: | ||
// - Get time since last conversation | ||
bp.dialogEngine.onBeforeEnd(async (ctx, next) => { | ||
const { stateId } = ctx | ||
|
||
if (!stateId.includes(':')) { | ||
// Unknown platform / can't extract userId | ||
return next() | ||
} | ||
|
||
const position = await bp.dialogEngine.getCurrentPosition(stateId) | ||
await bp.users.tag(stateId, USER_TAG_CONVO_LAST, position && position.flow) | ||
|
||
next() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters