wingbot-mongodb

4.2.0 • Public • Published

MongoDB plugin for wingbot.ai

Contains storage for tokens, chat states, bot config and chat logs.


API

Classes

StateStorage

Storage for chat states

BotTokenStorage

Storage for webview tokens

ChatLogStorage

Storage for conversation logs

BotConfigStorage

Storage for wingbot.ai conversation config

AttachmentCache

Cache storage for Facebook attachments

AuditLogStorage

Storage for audit logs with signatures chain

NotificationsStorage
BaseStorage

Typedefs

State : object
StateCondition : object
Token : object
Db : module:mongodb/lib/db
Db : module:mongodb/lib/db
Collection : module:mongodb/lib/collection
Db : module:mongodb/lib/db
TrackingEvent : object
User : object
Meta : object
LogEntry : object
JwtVerifierPromise.<boolean>

JWT Verifier

AuditLogEntry : object
AuditLogCallbackPromise

Audit Log Callback

Target : Object
Subscribtion : Object
Campaign : object
Task : Object
Db : module:mongodb/lib/db
Collection : module:mongodb/lib/collection

StateStorage

Storage for chat states

Kind: global class

new StateStorage(mongoDb, collectionName, [log], isCosmo)

Param Type Default Description
mongoDb mongodb.Db | Object
collectionName string "states"
[log] Object console like logger
isCosmo boolean false

stateStorage.addCustomIndex(index, options)

Deprecated

Add custom indexing rule

Kind: instance method of StateStorage

Param Type
index object
options object
options.name string

stateStorage.getState(senderId, pageId) ⇒ Promise.<(State|null)>

Kind: instance method of StateStorage

Param Type
senderId string
pageId string

stateStorage.getOrCreateAndLock(senderId, pageId, [defaultState], [timeout]) ⇒ Promise.<object>

Load state from database and lock it to prevent another reads

Kind: instance method of StateStorage
Returns: Promise.<object> - - conversation state

Param Type Default Description
senderId string sender identifier
pageId string page identifier
[defaultState] object default state of the conversation
[timeout] number 300 given default state

stateStorage.getStates(condition, limit, lastKey) ⇒ Promise.<{Array.<data:State>, lastKey:string}>

Kind: instance method of StateStorage

Param Type Default
condition StateCondition
limit number 20
lastKey string null

stateStorage.saveState(state) ⇒ Promise.<object>

Save the state to database

Kind: instance method of StateStorage

Param Type Description
state object conversation state

BotTokenStorage

Storage for webview tokens

Kind: global class

new BotTokenStorage(mongoDb, collectionName)

Param Type Default
mongoDb mongodb.Db | Object
collectionName string "tokens"

botTokenStorage._collection : mongodb.Collection

Kind: instance property of BotTokenStorage

botTokenStorage._getCollection() ⇒ Promise.<mongodb.Collection>

Kind: instance method of BotTokenStorage

botTokenStorage.findByToken(token) ⇒ Promise.<(Token|null)>

Kind: instance method of BotTokenStorage

Param Type
token string

botTokenStorage.getOrCreateToken(senderId, pageId, createToken) ⇒ Promise.<(Token|null)>

Kind: instance method of BotTokenStorage

Param Type
senderId string
pageId string
createToken Object

ChatLogStorage

Storage for conversation logs

Kind: global class

new ChatLogStorage(mongoDb, collectionName, [log], [isCosmo], [secret])

Param Type Default Description
mongoDb Db | Object
collectionName string "chatlogs"
[log] Object console like logger
[isCosmo] boolean false
[secret] string | Promise.<string> null

chatLogStorage.getInteractions(senderId, pageId, [limit], [endAt], [startAt]) ⇒ Promise.<Array.<object>>

Interate history all limits are inclusive

Kind: instance method of ChatLogStorage

Param Type Default Description
senderId string
pageId string
[limit] number 10
[endAt] number iterate backwards to history
[startAt] number iterate forward to last interaction

chatLogStorage.log(senderId, responses, request, [metadata]) ⇒ Promise

Log single event

Kind: instance method of ChatLogStorage

Param Type Description
senderId string
responses Array.<object> list of sent responses
request object event request
[metadata] object request metadata

BotConfigStorage

Storage for wingbot.ai conversation config

Kind: global class

new BotConfigStorage(mongoDb, collectionName)

Param Type Default
mongoDb Db | Object
collectionName string "botconfig"

botConfigStorage._collection : Collection

Kind: instance property of BotConfigStorage

botConfigStorage._getCollection() ⇒ Promise.<Collection>

Kind: instance method of BotConfigStorage

botConfigStorage.api([onUpdate], [acl]) ⇒ Object

Returns botUpdate API for wingbot

Kind: instance method of BotConfigStorage

Param Type Description
[onUpdate] function async update handler function
[acl] function | Array.<string> acl configuration

botConfigStorage.invalidateConfig() ⇒ Promise

Invalidates current configuration

Kind: instance method of BotConfigStorage

botConfigStorage.getConfigTimestamp() ⇒ Promise.<number>

Kind: instance method of BotConfigStorage

botConfigStorage.updateConfig(newConfig, [id]) ⇒ Promise.<T>

Kind: instance method of BotConfigStorage

Param Type
newConfig T
[id] string

botConfigStorage.setConfig(id, newConfig)

Kind: instance method of BotConfigStorage

Param Type
id string
newConfig object

botConfigStorage.getConfig([id]) ⇒ Promise.<(object|null)>

Kind: instance method of BotConfigStorage

Param Type
[id] string

AttachmentCache

Cache storage for Facebook attachments

Kind: global class

new AttachmentCache(mongoDb, collectionName)

Param Type Default
mongoDb mongodb.Db | Object
collectionName string "attachments"

attachmentCache._collection : mongodb.Collection

Kind: instance property of AttachmentCache

attachmentCache._getCollection() ⇒ Promise.<mongodb.Collection>

Kind: instance method of AttachmentCache

attachmentCache.findAttachmentByUrl(url) ⇒ Promise.<(number|null)>

Kind: instance method of AttachmentCache

Param Type
url string

attachmentCache.saveAttachmentId(url, attachmentId) ⇒ Promise

Kind: instance method of AttachmentCache

Param Type
url string
attachmentId number

AuditLogStorage

Storage for audit logs with signatures chain

Kind: global class

new AuditLogStorage(mongoDb, collectionName, [log], [isCosmo], [secret], [jwtVerifier])

Param Type Default Description
mongoDb Db | Object
collectionName string "auditlog"
[log] Object console like logger
[isCosmo] boolean false
[secret] string | Promise.<string> null
[jwtVerifier] string | Promise.<string> null

auditLogStorage._jwtVerify : JwtVerifier

Kind: instance property of AuditLogStorage

auditLogStorage.callback : AuditLogCallback

Kind: instance property of AuditLogStorage

auditLogStorage.log(event, user, [meta], [wid], [type], [level], [date]) ⇒ Promise

Add a log

Kind: instance method of AuditLogStorage

Param Type Description
event TrackingEvent
user User
[meta] Meta
[wid] string workspace ID
[type] string
[level] string
[date] Date

auditLogStorage.list([wid], [fromSeq], [limit]) ⇒ Promise.<Array.<LogEntry>>

Kind: instance method of AuditLogStorage

Param Type Default Description
[wid] string workspace id
[fromSeq] number 0 for paging
[limit] number 40

NotificationsStorage

Kind: global class

new NotificationsStorage(mongoDb, collectionsPrefix, [log], isCosmo)

Param Type Default Description
mongoDb mongodb.Db | Object
collectionsPrefix string
[log] Object console like logger
isCosmo boolean false

notificationsStorage._collections : Map.<string, Promise.<mongodb.Collection>>

Kind: instance property of NotificationsStorage

notificationsStorage._getCollection(collectionName) ⇒ Promise.<mongodb.Collection>

Kind: instance method of NotificationsStorage

Param Type
collectionName string

notificationsStorage.pushTasks(tasks) ⇒ Promise.<Array.<Task>>

Kind: instance method of NotificationsStorage

Param Type
tasks object

notificationsStorage.getUnsuccessfulSubscribersByCampaign(campaignId, [sentWithoutReaction], [pageId])

Kind: instance method of NotificationsStorage

Param Type Default
campaignId string
[sentWithoutReaction] boolean false
[pageId] string null

notificationsStorage.getTaskById(taskId) ⇒ Promise.<(Task|null)>

Return Task By Id

Kind: instance method of NotificationsStorage

Param Type
taskId string

notificationsStorage.updateTask(taskId, data)

Kind: instance method of NotificationsStorage

Param Type
taskId string
data object

notificationsStorage.getSentTask(pageId, senderId, campaignId) ⇒ Promise.<(Task|null)>

Get last sent task from campaign

Kind: instance method of NotificationsStorage

Param Type
pageId string
senderId string
campaignId string

notificationsStorage.getSentCampagnIds(pageId, senderId, checkCampaignIds) ⇒ Promise.<Array.<string>>

Kind: instance method of NotificationsStorage

Param Type
pageId string
senderId string
checkCampaignIds Array.<string>

notificationsStorage.updateTasksByWatermark(senderId, pageId, watermark, eventType, ts) ⇒ Promise.<Array.<Task>>

Kind: instance method of NotificationsStorage

Param Type
senderId string
pageId string
watermark number
eventType 'read' | 'delivery'
ts number

notificationsStorage.upsertCampaign(campaign, [updateCampaign]) ⇒ Promise.<Campaign>

Kind: instance method of NotificationsStorage

Param Type Default
campaign object
[updateCampaign] object

notificationsStorage.removeCampaign(campaignId) ⇒ Promise

Kind: instance method of NotificationsStorage

Param Type
campaignId string

notificationsStorage.incrementCampaign(campaignId, increment) ⇒ Promise

Kind: instance method of NotificationsStorage

Param Type
campaignId string
increment object

notificationsStorage.updateCampaign(campaignId, data) ⇒ Promise.<(Campaign|null)>

Kind: instance method of NotificationsStorage

Param Type
campaignId string
data object

notificationsStorage.popCampaign([now]) ⇒ Promise.<(Campaign|null)>

Kind: instance method of NotificationsStorage

Param Type
[now] number

notificationsStorage.getCampaignById(campaignId) ⇒ Promise.<(null|Campaign)>

Kind: instance method of NotificationsStorage

Param Type
campaignId string

notificationsStorage.getCampaignByIds(campaignIds) ⇒ Promise.<Array.<Campaign>>

Kind: instance method of NotificationsStorage

Param Type
campaignIds Array.<string>

notificationsStorage.getCampaigns(condition, [limit], [lastKey]) ⇒ Promise.<{Array.<data:Campaign>, lastKey:string}>

Kind: instance method of NotificationsStorage

Param Type Default
condition object
[limit] number
[lastKey] object

notificationsStorage.subscribe(senderId, pageId, tag) ⇒ Promise

Kind: instance method of NotificationsStorage

Param Type
senderId string
pageId string
tag string

notificationsStorage.unsubscribe(senderId, pageId, [tag]) ⇒ Promise.<Array.<string>>

Kind: instance method of NotificationsStorage

Param Type Default
senderId string
pageId string
[tag] string null

notificationsStorage.getSubscribtionsCount(include, exclude, [pageId]) ⇒ Promise.<number>

Kind: instance method of NotificationsStorage

Param Type Default
include Array.<string>
exclude Array.<string>
[pageId] string null

notificationsStorage.getSubscribtions(include, exclude, limit, [pageId], lastKey) ⇒ Promise.<{data: Array.<Target>, lastKey: string}>

Kind: instance method of NotificationsStorage

Param Type Default
include Array.<string>
exclude Array.<string>
limit number
[pageId] string null
lastKey *

notificationsStorage.getSenderSubscribtions(senderId, pageId) ⇒ Promise.<Array.<string>>

Kind: instance method of NotificationsStorage

Param Type
senderId string
pageId string

BaseStorage

Kind: global class

new BaseStorage(mongoDb, collectionName, [log], [isCosmo])

Param Type Default Description
mongoDb Db | Object
collectionName string
[log] Object console like logger
[isCosmo] boolean false

Example

const { BaseStorage } = require('winbot-mongodb');

class MyCoolDataStorage extends BaseStorage {

    constructor (mongoDb, collectionName = 'myCoolData', log = console, isCosmo = false) {
         super(mongoDb, collectionName, log, isCosmo);

         this.addIndex({
             foo: -1
         }, {
             name: 'foo_1'
         });

         this.addIndex({
             bar: -1,
             baz: 1
         }, {
             name: 'bar_-1_baz_1'
         });
    }

}

baseStorage._collection : Collection | Promise.<Collection>

Kind: instance property of BaseStorage

baseStorage.addFixtureDoc(...objects)

Insert defalt document to DB

Kind: instance method of BaseStorage

Param Type
...objects any

baseStorage.addIndex(index, options)

Add custom indexing rule

Kind: instance method of BaseStorage

Param Type
index object
options mongodb.IndexOptions

baseStorage._getCollection() ⇒ Promise.<Collection>

Returns the collection to operate with

Kind: instance method of BaseStorage

State : object

Kind: global typedef
Properties

Name Type
senderId string
pageId string
state object

StateCondition : object

Kind: global typedef
Properties

Name Type
[search] string

Token : object

Kind: global typedef
Properties

Name Type
senderId string
pageId string
token string

Db : module:mongodb/lib/db

Kind: global typedef

Db : module:mongodb/lib/db

Kind: global typedef

Collection : module:mongodb/lib/collection

Kind: global typedef

Db : module:mongodb/lib/db

Kind: global typedef

TrackingEvent : object

Kind: global typedef
Properties

Name Type Default
[type] string "'audit'"
category string
action string
[label] string
[payload] object

User : object

Kind: global typedef
Properties

Name Type Description
[id] string
[senderId] string
[pageId] string
[jwt] string jwt to check the authorship

Meta : object

Kind: global typedef
Properties

Name Type Description
[ip] string
[ua] string
[ro] string referrer

LogEntry : object

Kind: global typedef
Properties

Name Type Default Description
date string ISO date
delta number time skew in ms if there was a write conflict
[eventType] string "'audit'"
category string
action string
[label] string
[payload] object
level string (Critical
ok boolean signature matches
seq number sequence number
type string (Error
user User
wid string workspace id
meta Meta

JwtVerifier ⇒ Promise.<boolean>

JWT Verifier

Kind: global typedef

Param Type
token string
userId string
[user] User

AuditLogEntry : object

Kind: global typedef
Properties

Name Type Default Description
date string ISO date
[eventType] string "'audit'"
category string
action string
[label] string
[payload] object
level string (Critical
type string (Error
user User
wid string workspace id
meta Meta

AuditLogCallback ⇒ Promise

Audit Log Callback

Kind: global typedef

Param Type
entry AuditLogEntry

Target : Object

Kind: global typedef
Properties

Name Type
senderId string
pageId string

Subscribtion : Object

Kind: global typedef
Properties

Name Type
senderId string
pageId string
subs Array.<string>

Campaign : object

Kind: global typedef
Properties

Name Type Description
id string
name string Tatgeting
include Array.<string>
exclude Array.<string> Stats
sent number
succeeded number
failed number
unsubscribed number
delivery number
read number
notSent number
leaved number
queued number Interaction
action string
[data] object Setup
sliding boolean
slide number
slideRound number
active boolean
in24hourWindow boolean
startAt number

Task : Object

Kind: global typedef
Properties

Name Type Description
id string
pageId string
senderId string
campaignId string
enqueue number
[read] number
[delivery] number
[sent] number
[insEnqueue] number
[reaction] boolean user reacted
[leaved] number time the event was not sent because user left

Db : module:mongodb/lib/db

Kind: global typedef

Collection : module:mongodb/lib/collection

Kind: global typedef

Package Sidebar

Install

npm i wingbot-mongodb

Weekly Downloads

56

Version

4.2.0

License

MIT

Unpacked Size

290 kB

Total Files

22

Last publish

Collaborators

  • wingbot.ai
  • zdehasek