You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bot Framework V3(C#)向离线Skype for Business用户发主动消息

Answers to Your Bot Framework V3 Questions

1. Can I send messages to offline users?

Yes, you can send messages to offline users in Skype for Business using Bot Framework V3. The Skype for Business channel handles queuing these messages automatically—your bot sends the message as normal, and the platform stores it until the user comes back online, at which point it gets delivered. No extra logic is needed on your end for this behavior.

2. Does the provided code start a conversation, and how to use it?

Your code has the right core idea, but there are a few adjustments to ensure it works reliably. Here's a breakdown of how it operates and the fixes you'll need:

Key Fixes & Observations:

  • 1:1 vs Group Conversation: You set isGroup: true in ConversationParameters, which is for group chats. For a direct message to a single user, change this to isGroup: false.
  • Use the Correct Connector for Sending: You initialized connectorSend with the conversation-specific ServiceUrl from the response, but then used the original connector (tied to the platform URI) to send the message. Swap to connectorSend for the final SendToConversationAsync call—this ensures you're using the right endpoint for the active conversation.
  • Simplify Participant List: For 1:1 chats, you don't need to populate the participants list; the Recipient field in your message is sufficient.

How to Operate the Code:

  1. Host the Web API: Deploy this controller in an ASP.NET Web API project compatible with Bot Framework V3.
  2. Configure Credentials: Ensure your bot's MicrosoftAppId and MicrosoftAppPassword are set in your project config (or pass them directly to the ConnectorClient constructor if you're not using defaults). These are tied to your Skype for Business bot registration.
  3. Trigger the Endpoint: Send a POST request to https://your-api-domain/api/CustomWebAPI with any dummy string in the request body (since userobj isn't used in your current code). This will initiate the conversation and send the "text message" to the specified user.

Revised Code Snippet (with fixes):

public class CustomWebAPIController : ApiController
{
    [HttpPost]
    [Route("api/CustomWebAPI")]
    public async Task<HttpResponseMessage> SendMessage([FromBody]string userobj)
    {
        try
        {
            string trustServiceUri = "https://api.skypeforbusiness.com/platformservice/botframework";
            MicrosoftAppCredentials.TrustServiceUrl(trustServiceUri);
            
            // Initialize connector to create the conversation
            ConnectorClient connector = new ConnectorClient(new Uri(trustServiceUri));
            
            // Configure for 1:1 conversation
            ConversationParameters parameters = new ConversationParameters(
                isGroup: false,
                bot: new ChannelAccount("sip:XXXXXbot@XXXXX.onmicrosoft.com", "Bot"),
                participants: null, // Unnecessary for 1:1 chats
                topicName: "TestTopic"
            );
            
            ConversationResourceResponse response = await connector.Conversations.CreateConversationAsync(parameters);
            
            // Use conversation-specific ServiceUrl to send messages
            ConnectorClient connectorSend = new ConnectorClient(new Uri(response.ServiceUrl));
            
            IMessageActivity msg = Activity.CreateMessageActivity();
            msg.Recipient = new ChannelAccount("sip:XXXXX@XXXXX.onmicrosoft.com", "Agent");
            msg.Text = "text message";
            msg.Locale = "en-US";
            msg.From = new ChannelAccount("sip:XXXXXbot@XXXXX.onmicrosoft.com", "Bot");
            msg.Type = ActivityTypes.Message;
            msg.Timestamp = DateTime.UtcNow;
            msg.ChannelId = "skypeforbusiness";
            msg.ServiceUrl = response.ServiceUrl;
            msg.Conversation = new ConversationAccount(isGroup: false, id: response.Id, name: null);
            
            // Send message using the conversation-specific connector
            await connectorSend.Conversations.SendToConversationAsync((Activity)msg);
            
            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
        }
    }
}

Additional Tips:

  • Double-check that the SIP addresses for your bot and target user are correct (replace XXXXX with your actual tenant and user details).
  • If using Azure AD, confirm your bot has the necessary permissions to interact with Skype for Business.
  • Test the endpoint with tools like Postman to verify conversation creation and message delivery work as expected.

内容的提问来源于stack exchange,提问作者Jeff_hu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:56:50