Botframework-sdk: Need to cast to send Activity class?

Created on 10 Oct 2016  路  5Comments  路  Source: microsoft/botframework-sdk

We need to send proactive messages to users. This code, as written, requires us to cast our IMessageActivity to the Activity class type, which could lead to a runtime error when moving to future releases to the framework:

var message = Activity.CreateMessageActivity(); // message is IMessageActivity
message.Text = "Hello";
await connector.Conversations.SendToConversationAsync((Activity)message); // need to cast to Activity

Is there a way around this? And ideally, shouldn't SendToConversationAsync and friends instead take in an IActivity interface to support this, rather than Activity? Thanks in advance!

All 5 comments

FTR, within an IDialog you can do this:

        private async Task WaitForMessageAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
        {
            var newMessage = context.MakeMessage();
...
            context.PostAsync(newMessage);
...

and in a Controller you can do:

        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            var reply = activity.CreateReply();
...
            Conversation.SendAsync(reply, new MyDialog());
...

can you help me understand why you need to use Activity.CreateMessageActivity() and where you're creating/getting connector?

Thanks @brandonh-msft! We actually _are_ using IDialogContext.PostAsync from within our IDialog, which is good to verify 馃槂

We have a requirement that our bot follow-up with a user at a future date, so we're storing details about the conversation and creating a new ConnectorClient using the Uri-based constructor. We don't have access to an IDialogContext at the time message needs to be sent, which is why we're using the approach above.

rock on. I do think using IMessageActivity like all the other post/sends is a good change to make. hopefully @msft-shahins agrees and we'll see the change in the future.

Thanks for your feedback, @cglong!
You might even consider going the extra mile and making the change in a Pull Request and we can discuss/approve it there and you'll get Internets awarded ;)

@cglong Activity is implementing all the interfaces inheriting form IActivity (different activity types supported by bot framework): https://github.com/Microsoft/BotBuilder/blob/develop/CSharp/Library/Microsoft.Bot.Connector/ActivityEx.cs#L20.

If you look at the CreateMessageActivity() implementation it instantiates Activity object with the right ActivityType and upcast it to IMessageActivity.

SendToConversation() is an extension method written to wrap the auto-generated code by autorest from the connector api swagger. You can either write another extension method to do the cast or change swagger to have the autogenerated code be based on IActivity interface. I am not sure if the later is possible (needs some investigation). Also, if you are worried about the overhead of cast, I recommend using as operator over the hard cast to Activity object: http://www.codeproject.com/Articles/8052/Type-casting-impact-over-execution-performance-in.

Closing due to age and the question being answered.

Was this page helpful?
0 / 5 - 0 ratings