Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 382f657

Browse files
Added support for Command activities (#1092)
1 parent 82e0747 commit 382f657

File tree

5 files changed

+265
-0
lines changed

5 files changed

+265
-0
lines changed

libraries/bot-builder/src/main/java/com/microsoft/bot/builder/ActivityHandler.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ public CompletableFuture<Void> onTurn(TurnContext turnContext) {
8585
case ActivityTypes.INSTALLATION_UPDATE:
8686
return onInstallationUpdate(turnContext);
8787

88+
case ActivityTypes.COMMAND:
89+
return onCommandActivity(turnContext);
90+
91+
case ActivityTypes.COMMAND_RESULT:
92+
return onCommandResultActivity(turnContext);
93+
8894
case ActivityTypes.END_OF_CONVERSATION:
8995
return onEndOfConversationActivity(turnContext);
9096

@@ -528,6 +534,62 @@ protected CompletableFuture<Void> onInstallationUpdate(TurnContext turnContext)
528534
}
529535
}
530536

537+
/**
538+
* Invoked when a command activity is received when the base behavior of
539+
* {@link ActivityHandler#onTurn(TurnContext)} is used. Commands are requests to perform an
540+
* action and receivers typically respond with one or more commandResult
541+
* activities. Receivers are also expected to explicitly reject unsupported
542+
* command activities.
543+
*
544+
* @param turnContext A strongly-typed context Object for this
545+
* turn.
546+
*
547+
* @return A task that represents the work queued to execute.
548+
*
549+
* When the {@link ActivityHandler#onTurn(TurnContext)} method receives a command activity,
550+
* it calls this method. In a derived class, override this method to add
551+
* logic that applies to all comand activities. Add logic to apply before
552+
* the specific command-handling logic before the call to the base class
553+
* {@link ActivityHandler#onCommandActivity(TurnContext)} method. Add
554+
* logic to apply after the specific command-handling logic after the call
555+
* to the base class
556+
* {@link ActivityHandler#onCommandActivity(TurnContext)} method. Command
557+
* activities communicate programmatic information from a client or channel
558+
* to a bot. The meaning of an command activity is defined by the
559+
* name property, which is meaningful within the scope of a channel.
560+
*/
561+
protected CompletableFuture<Void> onCommandActivity(TurnContext turnContext) {
562+
return CompletableFuture.completedFuture(null);
563+
}
564+
565+
/**
566+
* Invoked when a CommandResult activity is received when the
567+
* base behavior of {@link ActivityHandler#onTurn(TurnContext)} is used. CommandResult
568+
* activities can be used to communicate the result of a command execution.
569+
*
570+
* @param turnContext A strongly-typed context Object for this
571+
* turn.
572+
*
573+
* @return A task that represents the work queued to execute.
574+
*
575+
* When the {@link ActivityHandler#onTurn(TurnContext)} method receives a CommandResult
576+
* activity, it calls this method. In a derived class, override this method
577+
* to add logic that applies to all comand activities. Add logic to apply
578+
* before the specific CommandResult-handling logic before the call to the
579+
* base class
580+
* {@link ActivityHandler#onCommandResultActivity(TurnContext)}
581+
* method. Add logic to apply after the specific CommandResult-handling
582+
* logic after the call to the base class
583+
* {@link ActivityHandler#onCommandResultActivity(TurnContext)}
584+
* method. CommandResult activities communicate programmatic information
585+
* from a client or channel to a bot. The meaning of an CommandResult
586+
* activity is defined by the name property,
587+
* which is meaningful within the scope of a channel.
588+
*/
589+
protected CompletableFuture<Void> onCommandResultActivity(TurnContext turnContext) {
590+
return CompletableFuture.completedFuture(null);
591+
}
592+
531593
/**
532594
* Override this in a derived class to provide logic specific to ActivityTypes.InstallationUpdate
533595
* activities with 'action' set to 'add'.

libraries/bot-builder/src/test/java/com/microsoft/bot/builder/ActivityHandlerTests.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,42 @@ public void TestEventNullNameAsync() {
408408
Assert.assertEquals("onEvent", bot.getRecord().get(1));
409409
}
410410

411+
@Test
412+
public void TestCommandActivityType() {
413+
Activity activity = new Activity(ActivityTypes.COMMAND);
414+
activity.setName("application/test");
415+
CommandValue<Object> commandValue = new CommandValue<Object>();
416+
commandValue.setCommandId("Test");
417+
commandValue.setData(new Object());
418+
activity.setValue(commandValue);
419+
420+
TurnContext turnContext = new TurnContextImpl(new NotImplementedAdapter(), activity);
421+
422+
TestActivityHandler bot = new TestActivityHandler();
423+
bot.onTurn(turnContext).join();
424+
425+
Assert.assertEquals(bot.getRecord().size(), 1);
426+
Assert.assertEquals("onCommandActivity", bot.record.get(0));
427+
}
428+
429+
@Test
430+
public void TestCommandResultActivityType() {
431+
Activity activity = new Activity(ActivityTypes.COMMAND_RESULT);
432+
activity.setName("application/test");
433+
CommandResultValue<Object> commandValue = new CommandResultValue<Object>();
434+
commandValue.setCommandId("Test");
435+
commandValue.setData(new Object());
436+
activity.setValue(commandValue);
437+
438+
TurnContext turnContext = new TurnContextImpl(new NotImplementedAdapter(), activity);
439+
440+
TestActivityHandler bot = new TestActivityHandler();
441+
bot.onTurn(turnContext).join();
442+
443+
Assert.assertEquals(bot.getRecord().size(), 1);
444+
Assert.assertEquals("onCommandResultActivity", bot.record.get(0));
445+
}
446+
411447
@Test
412448
public void TestUnrecognizedActivityType() {
413449
Activity activity = new Activity() {
@@ -570,5 +606,17 @@ protected CompletableFuture onUnrecognizedActivityType(TurnContext turnContext)
570606
return super.onUnrecognizedActivityType(turnContext);
571607
}
572608

609+
@Override
610+
protected CompletableFuture onCommandActivity(TurnContext turnContext){
611+
record.add("onCommandActivity");
612+
return super.onCommandActivity(turnContext);
613+
}
614+
615+
@Override
616+
protected CompletableFuture onCommandResultActivity(TurnContext turnContext) {
617+
record.add("onCommandResultActivity");
618+
return super.onCommandResultActivity(turnContext);
619+
}
620+
573621
}
574622
}

libraries/bot-schema/src/main/java/com/microsoft/bot/schema/ActivityTypes.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ private ActivityTypes() {
3131
*/
3232
public static final String TYPING = "typing";
3333

34+
/**
35+
* Enum value for Command Activities.
36+
*/
37+
public static final String COMMAND = "command";
38+
39+
/**
40+
* Enum value for Command Result Activities.
41+
*/
42+
public static final String COMMAND_RESULT = "commandResult";
43+
3444
/**
3545
* Enum value endOfConversation.
3646
*/
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MT License.
3+
4+
package com.microsoft.bot.schema;
5+
6+
import com.fasterxml.jackson.annotation.JsonInclude;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
9+
/**
10+
* The value field of a CommandResultActivity contains metadata related
11+
* to a command result.An optional extensible data payload may be included if
12+
* defined by the command result activity name. The presence of an error field
13+
* indicates that the original command failed to complete.
14+
* @param <T> The type of the CommandResultValue.
15+
*/
16+
public class CommandResultValue<T> {
17+
18+
@JsonProperty(value = "commandId")
19+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
20+
private String commandId;
21+
22+
@JsonProperty(value = "data")
23+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
24+
private T data;
25+
26+
@JsonProperty(value = "error")
27+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
28+
private Error error;
29+
30+
/**
31+
* Gets the id of the command.
32+
* @return the CommandId value as a String.
33+
*/
34+
public String getCommandId() {
35+
return this.commandId;
36+
}
37+
38+
/**
39+
* Sets the id of the command.
40+
* @param withCommandId The CommandId value.
41+
*/
42+
public void setCommandId(String withCommandId) {
43+
this.commandId = withCommandId;
44+
}
45+
46+
/**
47+
* Gets the data field containing optional parameters specific to
48+
* this command result activity, as defined by the name. The value of the
49+
* data field is a complex type.
50+
* @return the Data value as a T.
51+
*/
52+
public T getData() {
53+
return this.data;
54+
}
55+
56+
/**
57+
* Sets the data field containing optional parameters specific to
58+
* this command result activity, as defined by the name. The value of the
59+
* data field is a complex type.
60+
* @param withData The Data value.
61+
*/
62+
public void setData(T withData) {
63+
this.data = withData;
64+
}
65+
66+
/**
67+
* Gets the optional error, if the command result indicates a
68+
* failure.
69+
* @return the Error value as a getError().
70+
*/
71+
public Error getError() {
72+
return this.error;
73+
}
74+
75+
/**
76+
* Sets the optional error, if the command result indicates a
77+
* failure.
78+
* @param withError The Error value.
79+
*/
80+
public void setError(Error withError) {
81+
this.error = withError;
82+
}
83+
84+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MT License.
3+
4+
package com.microsoft.bot.schema;
5+
6+
import com.fasterxml.jackson.annotation.JsonInclude;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
9+
/**
10+
* The value field of a CommandActivity contains metadata related to a
11+
* command.An optional extensible data payload may be included if defined by
12+
* the command activity name.
13+
* @param <T> The type of the CommandValue.
14+
*/
15+
public class CommandValue<T> {
16+
17+
@JsonProperty(value = "commandId")
18+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
19+
private String commandId;
20+
21+
@JsonProperty(value = "data")
22+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
23+
private T data;
24+
25+
/**
26+
* Gets the id of the command.
27+
* @return the CommandId value as a String.
28+
*/
29+
public String getCommandId() {
30+
return this.commandId;
31+
}
32+
33+
/**
34+
* Sets the id of the command.
35+
* @param withCommandId The CommandId value.
36+
*/
37+
public void setCommandId(String withCommandId) {
38+
this.commandId = withCommandId;
39+
}
40+
41+
/**
42+
* Gets the data field containing optional parameters specific to
43+
* this command activity, as defined by the name. The value of the data
44+
* field is a complex type.
45+
* @return the Data value as a T.
46+
*/
47+
public T getData() {
48+
return this.data;
49+
}
50+
51+
/**
52+
* Sets the data field containing optional parameters specific to
53+
* this command activity, as defined by the name. The value of the data
54+
* field is a complex type.
55+
* @param withData The Data value.
56+
*/
57+
public void setData(T withData) {
58+
this.data = withData;
59+
}
60+
61+
}

0 commit comments

Comments
 (0)