'How to use Bot Connector Client when working with Slack adapter

I am using BotRouting component mentioned here which uses BotConnector Client to send messages to channels directly without using turncontext.sendactivityasync. Right now I am using Slack adapter to send messages to slack app. When using slack adapter with /api/slack endpoint I am unable to Sendmessage to Slack app directly using BotConnector client. However, when I use Slack app connected through AzureBotFramework portal which uses the endpoint /api/messages I am able to send message to slack app using BotConnector client.

Is there any alternative to BotConnector client? Please mention me the corrections if I made any mistake connecting slack channel with adapter.

Here is the code I used to send message to Slack app.

using System;
using System.Threading.Tasks;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;

namespace BotConnectorExample
{
    class Program
    {
        private static string botClientId = "bot_id";
        private static string botSecret = "bot_secret"; 
        //Tiende a ser constante, pero puede cambiar
        private static string serviceUrl = "service_url";
        private static string conversationId = "conversation_id";
        private static string recipientId = "recipient_Id";
        private static string fromId = "from_Id";

        static async Task Main(string[] args)
        {
            ConnectorClient connectorClient = GetConnectorClient();
            try
            {
                await SendMessageAsync(connectorClient,
                    conversationId,
                    recipientId,
                    fromId,
                    "Mensaje desde la consola.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        }

        private static async Task SendMessageAsync(
            ConnectorClient connectorClient,
            string conversationId,
            string recipientId,
            string fromId,
            string message)
        {
            Activity messageActivity =
                new Activity();
            messageActivity.Type = ActivityTypes.Message;
            messageActivity.Text = message;
            messageActivity.ChannelId = Channels.Webchat;
            messageActivity.ServiceUrl = serviceUrl;
            messageActivity.Conversation = new ConversationAccount()
            {
                Id = conversationId
            };
            messageActivity.Recipient = new ChannelAccount()
            {
                Id = recipientId
            };
            messageActivity.From = new ChannelAccount()
            {
                Id = fromId
            };

            await connectorClient
                .Conversations
                .SendToConversationAsync(messageActivity);

        }

        static ConnectorClient GetConnectorClient()
        {

            MicrosoftAppCredentials appCredentials =
               new MicrosoftAppCredentials(botClientId, botSecret);
            MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);

            Uri uri = new Uri(serviceUrl);
            ConnectorClient connectorClient =
                new ConnectorClient(uri, appCredentials);

            return connectorClient;
        }

    }
}

I have used serviceUrl as https://slack.botframework.com/



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source