Skip to content

Commit a5386b2

Browse files
johnataylorstevengum
authored andcommitted
add MessageReaction to ActivityHandler (#1038)
1 parent 8073c25 commit a5386b2

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

libraries/botbuilder-core/src/activityHandler.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,37 @@ export class ActivityHandler {
108108
return this.on('MembersRemoved', handler);
109109
}
110110

111+
/**
112+
* Receives only MessageReaction activities, regardless of whether message reactions were added or removed
113+
* @remarks
114+
* MessageReaction activities are sent to the bot when a message reacion, such as 'like' or 'sad' are
115+
* associated with an activity previously sent from the bot.
116+
* @param handler BotHandler A handler function in the form async(context, next) => { ... }
117+
*/
118+
public onMessageReaction(handler: BotHandler): this {
119+
return this.on('MessageReaction', handler);
120+
}
121+
122+
/**
123+
* Receives only MessageReaction activities representing message reactions being added.
124+
* @remarks
125+
* context.activity.reactionsAdded will include at least one entry.
126+
* @param handler BotHandler A handler function in the form async(context, next) => { ... }
127+
*/
128+
public onReactionsAdded(handler: BotHandler): this {
129+
return this.on('ReactionsAdded', handler);
130+
}
131+
132+
/**
133+
* Receives only MessageReaction activities representing message reactions being removed.
134+
* @remarks
135+
* context.activity.reactionsRemoved will include at least one entry.
136+
* @param handler BotHandler A handler function in the form async(context, next) => { ... }
137+
*/
138+
public onReactionsRemoved(handler: BotHandler): this {
139+
return this.on('ReactionsRemoved', handler);
140+
}
141+
111142
/**
112143
* Receives all Event activities.
113144
* @remarks
@@ -216,6 +247,17 @@ export class ActivityHandler {
216247
}
217248
});
218249
break;
250+
case ActivityTypes.MessageReaction:
251+
await this.handle(context, 'MessageReaction', async () => {
252+
if (context.activity.reactionsAdded && context.activity.reactionsAdded.length > 0) {
253+
await this.handle(context, 'ReactionsAdded', runDialogs);
254+
} else if (context.activity.reactionsRemoved && context.activity.reactionsRemoved.length > 0) {
255+
await this.handle(context, 'ReactionsRemoved', runDialogs);
256+
} else {
257+
await runDialogs();
258+
}
259+
});
260+
break;
219261
case ActivityTypes.Event:
220262
await this.handle(context, 'Event', async () => {
221263
if (context.activity.name === 'tokens/response') {

libraries/botbuilder-core/tests/ActivityHandler.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,45 @@ describe('ActivityHandler', function() {
133133
processActivity({type: ActivityTypes.ConversationUpdate, membersRemoved: [{id: 1}]}, bot);
134134
});
135135

136+
it(`should fire onMessageReaction`, async function (done) {
137+
138+
const bot = new ActivityHandler();
139+
140+
bot.onMessageReaction(async(context, next) => {
141+
assert(true, 'onMessageReaction not called');
142+
done();
143+
await next();
144+
});
145+
146+
processActivity({type: ActivityTypes.MessageReaction}, bot);
147+
});
148+
149+
it(`should fire onReactionsAdded`, async function (done) {
150+
151+
const bot = new ActivityHandler();
152+
153+
bot.onReactionsAdded(async(context, next) => {
154+
assert(true, 'onReactionsAdded not called');
155+
done();
156+
await next();
157+
});
158+
159+
processActivity({type: ActivityTypes.MessageReaction, reactionsAdded: [{type: 'like'}]}, bot);
160+
});
161+
162+
it(`should fire onReactionsRemoved`, async function (done) {
163+
164+
const bot = new ActivityHandler();
165+
166+
bot.onReactionsRemoved(async(context, next) => {
167+
assert(true, 'onReactionsRemoved not called');
168+
done();
169+
await next();
170+
});
171+
172+
processActivity({type: ActivityTypes.MessageReaction, reactionsRemoved: [{type: 'like'}]}, bot);
173+
});
174+
136175
it(`should fire onEvent`, async function (done) {
137176

138177
const bot = new ActivityHandler();

0 commit comments

Comments
 (0)