Skip to content

Commit 0f3a426

Browse files
committed
Added details about the implementation
1 parent 3cb0ad6 commit 0f3a426

File tree

1 file changed

+77
-5
lines changed

1 file changed

+77
-5
lines changed

README.md

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A possible use case for this type of a bot would be a customer service scenario
77
where the bot relays the messages between a customer and a customer service agent.
88

99

10-
### Running and testing ###
10+
## Running and testing ##
1111

1212
To test the bot, publish it and connect it to the channels of your choice.
1313
If you are new to bots, please familiarize yourself first with the basics
@@ -25,7 +25,7 @@ can use [ngrok](https://ngrok.com/) tunneling software:
2525

2626
See also: [Microsoft Bot Framework Emulator wiki](https://github.com/microsoft/botframework-emulator/wiki/Getting-Started)
2727

28-
#### The flow ####
28+
### The flow ###
2929

3030
| Emulator with ngrok | Slack |
3131
| ------------------- | ----- |
@@ -35,11 +35,83 @@ See also: [Microsoft Bot Framework Emulator wiki](https://github.com/microsoft/b
3535
| ![Conversation in emulator](/Documentation/Screenshots/ConversationInEmulator.png?raw=true) | ![Conversation in Slack](/Documentation/Screenshots/ConversationInSlack.png?raw=true) |
3636

3737

38-
### Implementation ###
38+
## Implementation ##
3939

40-
TBD
40+
### Terminology ###
4141

42-
### Acknowledgements ###
42+
| Term | Description |
43+
| ---- | ----------- |
44+
| Aggregation (channel) | A channel where the chat requests are sent. The users in the aggregation channel can accept the requests. |
45+
| Engagement | Is created when a request is accepted - the acceptor and the one accepted form an engagement (1:1 chat where the bot relays the messages between the users). |
46+
| Party | A user/bot in a specific conversation. |
47+
48+
### Classes ###
49+
50+
**[Party](/IntermediatorBotSample/MessageRouting/Party.cs)** holds the details
51+
of specific user/bot in a specific conversation. Note that the bot collects
52+
parties from all the conversations it's in and there will be a `Party` instance
53+
of a user/bot for each conversation (i.e. there can be multiple parties for a
54+
single user/bot). One can think of `Party` as a full address the bot needs in
55+
order to send a message to the user in a conversation. The `Party` instances are
56+
stored in routing data.
57+
58+
**[RoutingData](/IntermediatorBotSample/MessageRouting/RoutingData.cs)**
59+
contains the parties (users/bot), aggregation channel details, the list of
60+
engaged parties and pending requests. **Note** that this data should be stored
61+
in e.g. a blob storage! For testing it is OK to have the data in memory.
62+
63+
**[MessageRouterManager](/IntermediatorBotSample/MessageRouting/MessageRouterManager.cs)**
64+
is the main class of the sample. It manages the routing data and handles
65+
commands to the bot and executes the actual message mediation between the
66+
parties engaged in a conversation. The most important methods in this class
67+
are as follows:
68+
69+
* `AddParty`: Adds a new party to the routing data. It is recommended to use `MakeSurePartiesAreTracked` instead of this for adding parties.
70+
* `RemoveParty`: Removes all the instances related to the given party from the routing data (since there can be multiple - one for each conversation).
71+
* `MakeSurePartiesAreTracked`: A convenient method for adding parties. The given parties are added if they are new.
72+
* `IntiateEngagement`: Creates and posts a new chat request.
73+
* `AddEngagement`: Establishes an engagement between the given parties. This method is called when a chat request is accepted.
74+
* `HandleMessageAsync`: Handles the incoming messages: Creates chat requests if needed and relays the messages between engaged parties.
75+
* `HandleDirectCommandToBot`: Handles bot commands.
76+
77+
### Taking the classes into use ###
78+
79+
The most convenient place to use the aforementioned classes is in the
80+
**[MessagesController](/IntermediatorBotSample/Controllers/MessagesController.cs)**
81+
class - you can first call the methods in `MessageRouterManager` and, for
82+
instance, if no action is taken by the manager, you can forward the `Activity`
83+
to a `Dialog`:
84+
85+
```cs
86+
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
87+
{
88+
if (activity.Type == ActivityTypes.Message)
89+
{
90+
// Get the message router manager instance
91+
MessageRouterManager messageRouterManager = MessageRouterManager.Instance;
92+
93+
// Make we have the details of the sender and the receiver (bot) stored
94+
messageRouterManager.MakeSurePartiesAreTracked(activity);
95+
96+
// Check for possible commands first
97+
if (await messageRouterManager.HandleDirectCommandToBotAsync(activity) == false)
98+
{
99+
// No command to the bot was issued so it must be a message then
100+
messageRouterManager.HandleMessageAsync(activity);
101+
}
102+
}
103+
else
104+
{
105+
HandleSystemMessage(activity);
106+
}
107+
108+
var response = Request.CreateResponse(HttpStatusCode.OK);
109+
return response;
110+
}
111+
```
112+
113+
114+
## Acknowledgements ##
43115

44116
Although you can't see it in the change history,
45117
[Edouard Mathon](https://github.com/edouard-mathon) added

0 commit comments

Comments
 (0)