Skip to content

Commit

Permalink
feat(builtin): new users actions
Browse files Browse the repository at this point in the history
Added getTimeSinceLastConversation() and getTotalNumberOfConversations()
  • Loading branch information
slvnperron committed May 9, 2018
1 parent 43d725c commit b068be3
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/core/botpress-builtins/package.json
Expand Up @@ -23,7 +23,9 @@
"access": "public"
},
"dependencies": {
"joi": "^13.3.0",
"lodash": "^4.17.10",
"mime": "^2.3.1"
"mime": "^2.3.1",
"moment": "^2.22.1"
}
}
2 changes: 0 additions & 2 deletions packages/core/botpress-builtins/src/actions/index.js
@@ -1,8 +1,6 @@
export * from './users'

// ** Users **
// Get time since last conversation
// Get total number of conversations
// Get profile

// ** Storage **
Expand Down
46 changes: 46 additions & 0 deletions packages/core/botpress-builtins/src/actions/users.js
@@ -1,6 +1,9 @@
import Joi from 'joi'
import moment from 'moment'
import _ from 'lodash'

import baseAction, { Annotate } from './base'
import { USER_TAG_CONVO_COUNT, USER_TAG_CONVO_LAST } from '../common'

export const tagUser = baseAction(
async (bp, state, event, { tag, value }) => {
Expand Down Expand Up @@ -34,3 +37,46 @@ export const untagUser = baseAction(
.description('The name of the tag. Case insensitive, will always be uppercased.')
})
)

export const getTimeSinceLastConversation = baseAction(
async (bp, state, event, { unit }) => {
const result = await bp.users.getTag(event.user.id, USER_TAG_CONVO_LAST, true)
const tagged_on = (result && result.tagged_on) || new Date()
const since = moment().diff(moment(tagged_on), unit)

return {
...state,
$r: since
}
},
Joi.object().keys({
...Annotate(
'Users',
'Get elasped time since last conversation',
'Returns the time since last conversation in the unit of your choice. -> (out to `$r`)'
),
unit: Joi.string()
.optional()
.valid(['days', 'hours', 'minutes', 'seconds', 'months', 'years'])
.default('days')
.description('The unit of time elasped.')
})
)

export const getTotalNumberOfConversations = baseAction(
async (bp, state, event, { unit }) => {
const count = await bp.users.getTag(event.user.id, USER_TAG_CONVO_COUNT)

return {
...state,
$r: parseInt(count || 0)
}
},
Joi.object().keys({
...Annotate(
'Users',
'Get total number of conversations',
'Returns the total number of conversations this user had with the bot -> (out to `$r`)'
)
})
)
2 changes: 2 additions & 0 deletions packages/core/botpress-builtins/src/common.js
@@ -0,0 +1,2 @@
export const USER_TAG_CONVO_COUNT = 'BUILTIN_CONVO_COUNT'
export const USER_TAG_CONVO_LAST = 'BUILTIN_CONVO_LAST'
1 change: 1 addition & 0 deletions packages/core/botpress-builtins/src/index.js
@@ -1,3 +1,4 @@
export * as contentElements from './elements'
export contentRenderers from './renderers'
export * as actions from './actions'
export setup from './setup'
45 changes: 45 additions & 0 deletions packages/core/botpress-builtins/src/setup.js
@@ -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()
})
}
36 changes: 32 additions & 4 deletions packages/core/botpress-builtins/yarn.lock
Expand Up @@ -1573,6 +1573,10 @@ hoek@4.x.x:
version "4.2.1"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.1.tgz#9634502aa12c445dd5a7c5734b572bb8738aacbb"

hoek@5.x.x:
version "5.0.3"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-5.0.3.tgz#b71d40d943d0a95da01956b547f83c4a5b4a34ac"

home-or-tmp@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8"
Expand Down Expand Up @@ -1840,6 +1844,12 @@ isarray@1.0.0, isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"

isemail@3.x.x:
version "3.1.2"
resolved "https://registry.yarnpkg.com/isemail/-/isemail-3.1.2.tgz#937cf919002077999a73ea8b1951d590e84e01dd"
dependencies:
punycode "2.x.x"

isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
Expand Down Expand Up @@ -2191,6 +2201,14 @@ jest@^22.4.3:
import-local "^1.0.0"
jest-cli "^22.4.3"

joi@^13.3.0:
version "13.3.0"
resolved "https://registry.yarnpkg.com/joi/-/joi-13.3.0.tgz#4defd4333b539c5d10e444ab44f5a5583480f17c"
dependencies:
hoek "5.x.x"
isemail "3.x.x"
topo "3.x.x"

js-tokens@^3.0.0, js-tokens@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
Expand Down Expand Up @@ -2497,6 +2515,10 @@ mkdirp@^0.5.0, mkdirp@^0.5.1:
dependencies:
minimist "0.0.8"

moment@^2.22.1:
version "2.22.1"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.22.1.tgz#529a2e9bf973f259c9643d237fda84de3a26e8ad"

ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down Expand Up @@ -2851,14 +2873,14 @@ pseudomap@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pseudomap/-/pseudomap-1.0.2.tgz#f052a28da70e618917ef0a8ac34c1ae5a68286b3"

punycode@2.x.x, punycode@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.0.tgz#5f863edc89b96db09074bad7947bf09056ca4e7d"

punycode@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"

punycode@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.0.tgz#5f863edc89b96db09074bad7947bf09056ca4e7d"

qs@~6.5.1:
version "6.5.1"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.1.tgz#349cdf6eef89ec45c12d7d5eb3fc0c870343a6d8"
Expand Down Expand Up @@ -3430,6 +3452,12 @@ to-regex@^3.0.1, to-regex@^3.0.2:
regex-not "^1.0.2"
safe-regex "^1.1.0"

topo@3.x.x:
version "3.0.0"
resolved "https://registry.yarnpkg.com/topo/-/topo-3.0.0.tgz#37e48c330efeac784538e0acd3e62ca5e231fe7a"
dependencies:
hoek "5.x.x"

tough-cookie@>=2.3.3, tough-cookie@^2.3.3, tough-cookie@~2.3.3:
version "2.3.4"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.4.tgz#ec60cee38ac675063ffc97a5c18970578ee83655"
Expand Down

0 comments on commit b068be3

Please sign in to comment.