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: trueinConversationParameters, which is for group chats. For a direct message to a single user, change this toisGroup: false. - Use the Correct Connector for Sending: You initialized
connectorSendwith the conversation-specificServiceUrlfrom the response, but then used the originalconnector(tied to the platform URI) to send the message. Swap toconnectorSendfor the finalSendToConversationAsynccall—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
participantslist; theRecipientfield in your message is sufficient.
How to Operate the Code:
- Host the Web API: Deploy this controller in an ASP.NET Web API project compatible with Bot Framework V3.
- Configure Credentials: Ensure your bot's
MicrosoftAppIdandMicrosoftAppPasswordare set in your project config (or pass them directly to theConnectorClientconstructor if you're not using defaults). These are tied to your Skype for Business bot registration. - Trigger the Endpoint: Send a POST request to
https://your-api-domain/api/CustomWebAPIwith any dummy string in the request body (sinceuserobjisn'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
XXXXXwith 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
相关产品推荐
相关产品推荐

