---
# System prepended metadata

title: Rake User App Module

---

# Rake User App Module
https://hackmd.io/arwbfLg6SsamEb_31LVIcw?both[routingRules]

## communication protocol beetween Rake and User App application

### From Rake
#### Message
```
    event: 5,
    botId,
    sessionId,
    fromEntityId
    entityId,
    text,
    files,
    messageId,
    sentAt,
    isSystem,
    platformId
```
#### File
```
    event: 5,
    botId,
    sessionId,
    fromEntityId
    entityId,
    files,
    messageId,
    sentAt,
    isSystem,
    platformId
```
#### Session end
```
    event: 3,
    fromEntityId
    botId,
    sessionId,
    entityId
```
#### System event['assignedAgent', 'removeEntity']
```
    event: 8,
    teamEntityId,
    messageId,
    botId,
    sessionId,
    entityId,
    data,
    isDeleted
```
#### Read message
```
    event: 9,
    messageId,
    botId,
    sessionId,
    entityId
```


#### Typing preview
```
    event: 7,
    botId,
    fromEntityId,
    sessionId,
    entityId,
    text
```
### From UserApp to Rake
#### Message
```
    event: 4,
    botId
    sessionId
    entityId
    text
```
#### Message + Files [OLD VERSION]
```
    event: 10,
    botId
    sessionId
    entityId
    text,
    files
```
#### Files [OLD VERSION]
```
    event: 11,
    botId
    sessionId
    entityId
    files
```
#### Message + Files [NEW VERSION]
```
    event: 10,
    botId
    sessionId
    entityId
    text,
    files: [
         {fileid},
         ...
    ],
    sentAt
```
#### Files [NEW VERSION]
```
    event: 11,
    botId
    sessionId
    entityId
    files:[
        {fileid},
        ...
    ],
    sentAt
```
#### Session end
```
    event: 3
    botId,
    sessionId,
    entityId
```
#### Agent assign: 
 request POST /rake-user-app/agents 
```
{
    entityId,
    sessionId
}
```

## Module flow
### Connector part
1) Get info about timeout(after this time module ends work and sends message to another module if it is exists ) from workspace settings -> If in worspace settings field `sessionRouting.timeout` is empty or absent we use value from system config collection   `rakeUserApp.default.workspaceSettings.sessionRouting.timeout`
2) Send to the module object
  ``` {
      uri: moduleUrl,
      body: {
        event -- ['sendMessaage', 'beginProcess' and etc...],
        pack: {
          msg -- ['internal message']
          module: { fullName: parameters.fullName },
          data: {
            timeOut: result.timeOut * 1000, // 'from milli to seconds'
            workspaceId,
            params: `conversationFlowParameters`,
            extraInfo -- ['from parameters.json'],
          },
        },
      },
    };
```
### Module part
#### Process:
--- ***BeginProcess***
1) Check if this message related to internal chat (in case, when all entities in session record have platformId rakeUserApp) - if it is internal response with `Direct messaging` event, else - continue flow
2) Check timeout value (if it is empty or equal to 0 => answer with event name `process.end`)
3) Make request to the RakeUserApp
 uri: `${config.rakeUserApp.serviceUrl}/workspaces/${workspaceId}/agents/available`, 
 method: 'GET',
4) Check answer from request bellow
- if field `body.data.count` < 0 => answer with event name `process.end` and send message text `Sorry, we are unable to deliver this message at this time, please try again later.`
- if field `body.data.count` > 0 =>  answer with event name `send.message` and run timer with next functuanality:
    1) Check if session was accepted by agent (Check if history of this session has eventId which means that session was accepted by agent)
        - if true => delete timer
        - if false => 
            1) create system event `endSession` 
     uri: `${config.serverUrl}/modules/${fullName}/sessions/${sessionId}/event/end`,
          method: 'POST',
            3) send request to rakeUserApp  
    uri: `${config.rakeUserApp.serviceUrl}/sessions/${sessionId}/endSession?botId=${id}`,
          method: 'DELETE'
          
--- ***SendMessage***
1) Check platformId which sent this message
    - if userAppPlatform is sender => add to the message text `label` field
        - if message type equal to *end* => create custom event
            
## communication protocol beetween User App connector and User App module [https://hackmd.io/dZxyQXhWTOekugggYOW0cw]

