如何通过C#代码结合Cortana技能在Android实现LUIS触发式拨打电话?
Hey there! Let's break down how you can make this work—combining Microsoft Web Bot, LUIS, and Cortana to trigger phone calls on an Android device. Here's a step-by-step approach with actionable code snippets:
Your goal is totally achievable, but keep this in mind: the Web Bot itself can't directly call Android's dialer API. Instead, the workflow will be:
- LUIS identifies the user's "make a call" intent
- Your C# Bot processes the intent, extracts the target number/contact
- The Bot sends a structured response with a dial command
- An Android client (either Cortana or your custom app) listens for this response and triggers the device's dialer
First, set up your LUIS model to recognize call requests:
- Create an intent named
Intent_MakePhoneCall - Add entities like
PhoneNumber(to capture direct numbers) andContactName(to map to saved contacts) - Train and publish the LUIS model, then integrate its endpoint into your Web Bot
Here’s how to process the LUIS intent and prepare a structured response for the Android side:
// Assume you've already configured the LUIS recognizer in your Bot [LuisIntent("Intent_MakePhoneCall")] public async Task ProcessCallRequest(IDialogContext context, LuisResult result) { // Extract entities from the LUIS response var phoneNumberEntity = result.Entities.FirstOrDefault(e => e.Type == "PhoneNumber"); var contactEntity = result.Entities.FirstOrDefault(e => e.Type == "ContactName"); string targetNumber = string.Empty; if (phoneNumberEntity != null) { targetNumber = phoneNumberEntity.Entity; } else if (contactEntity != null) { // Map contact name to phone number (replace with your actual contact lookup logic) targetNumber = GetContactNumberFromName(contactEntity.Entity); } if (!string.IsNullOrEmpty(targetNumber)) { // Send a response with structured data for Android to parse var reply = context.MakeMessage(); reply.Text = $"Initiating call to {targetNumber}..."; // Add custom channel data to signal the dial command reply.ChannelData = new { Command = "TriggerDial", TargetNumber = targetNumber }; await context.PostAsync(reply); } else { await context.PostAsync("I couldn't find a valid phone number. Please share a contact name or direct number."); } context.Wait(MessageReceived); } // Mock method to fetch contact number (replace with your actual data source) private string GetContactNumberFromName(string contactName) { // Example: Pull from a backend database or integrated contact service return contactName.Equals("John", StringComparison.OrdinalIgnoreCase) ? "555-1234" : string.Empty; }
You’ll need an Android app (or extend Cortana’s functionality) to receive the Bot’s response and call the dialer. Here’s a Java example using the Microsoft Bot Framework Android SDK:
// Handle incoming Bot messages @Override public void onReceiveActivity(IActivity activity) { if (activity instanceof IMessageActivity) { IMessageActivity message = (IMessageActivity) activity; // Parse the custom channel data from the Bot if (message.getChannelData() != null) { try { JSONObject channelData = new JSONObject(message.getChannelData().toString()); String command = channelData.getString("Command"); if ("TriggerDial".equals(command)) { String phoneNumber = channelData.getString("TargetNumber"); // Launch the Android dialer Intent dialIntent = new Intent(Intent.ACTION_DIAL); dialIntent.setData(Uri.parse("tel:" + phoneNumber)); startActivity(dialIntent); } } catch (JSONException e) { Log.e("BotDialer", "Failed to parse channel data", e); } } } }
Don’t forget to add the necessary permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.CALL_PHONE" />
If you want to initiate the call directly (not just open the dialer), you’ll need to request this dangerous permission dynamically from the user.
To let users trigger this via Cortana:
- Register your Web Bot as a Cortana Skill in the Azure Portal
- Ensure Cortana passes the Bot’s responses to your Android client, or use Cortana’s native ability to handle voice commands for calls (though your custom logic gives more control over contact mapping)
内容的提问来源于stack exchange,提问作者user2271788

