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

如何使用C# Microsoft Bot Builder Location组件实现位置展示?

嘿,我来帮你把位置展示功能整合到你的Bot里!首先得确保你已经安装了Microsoft.Bot.Builder.Location这个NuGet包,这是实现功能的基础。接下来咱们一步步修改代码,完成从获取位置到展示的完整流程:

步骤1:启动位置选择对话框

在你的MessageReceivedStart方法中,我们需要启动LocationDialog引导用户选择或输入位置。你可以指定允许的国家范围、必填地址字段等参数,让位置采集更贴合你的需求。

步骤2:处理并展示位置结果

当用户完成位置选择后,Bot会收到一个Place对象,我们从中提取地址、坐标等信息,用富媒体卡片(比如HeroCard)来直观展示这些内容,还可以添加地图跳转按钮提升用户体验。

以下是完整的代码实现:

using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Location;
using Microsoft.Bot.Connector;
using System;
using System.Threading.Tasks;

namespace LocationBotDemo.Dialogs
{
    [Serializable]
    public class MainDialog : IDialog<object>
    {
        public async Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedStart);
        }

        public async Task MessageReceivedStart(IDialogContext context, IAwaitable<IMessageActivity> argument)
        {
            var message = await argument;
            
            // 初始化位置对话框,替换为你的Bing Maps API密钥(需在Azure申请)
            var locationDialog = new LocationDialog(new BingMapsApiKey("你的Bing Maps API密钥"), "en-US");
            // 设置必填的地址字段,可按需调整
            locationDialog.RequiredFields = LocationRequiredFields.StreetAddress | LocationRequiredFields.PostalCode;
            
            // 启动位置对话框,指定回调方法处理结果
            context.Call(locationDialog, LocationReceivedAsync);
        }

        private async Task LocationReceivedAsync(IDialogContext context, IAwaitable<Place> result)
        {
            var place = await result;
            
            if (place != null)
            {
                var reply = context.MakeMessage();
                var point = place.Point;

                // 用HeroCard构建位置展示卡片
                var locationCard = new HeroCard
                {
                    Title = "你选择的位置",
                    Subtitle = place.FormattedAddress,
                    Text = $"坐标:{point.Latitude}, {point.Longitude}",
                    Buttons = new[]
                    {
                        new CardAction(ActionTypes.OpenUrl, "查看地图", value: $"https://www.bing.com/maps?q={point.Latitude},{point.Longitude}")
                    }
                };
                
                reply.Attachments.Add(locationCard.ToAttachment());
                await context.PostAsync(reply);
            }
            else
            {
                await context.PostAsync("没有选择有效的位置哦~");
            }
            
            // 回到初始状态,等待用户下一条消息
            context.Wait(MessageReceivedStart);
        }
    }
}
关键细节说明
  • Bing Maps API密钥:必须替换成你自己的密钥,否则位置搜索和坐标解析功能无法正常工作,你可以在Azure门户中创建Bing Maps资源获取密钥。
  • 必填字段配置:LocationRequiredFields可以灵活组合,比如只需要城市就设置为LocationRequiredFields.Locality,根据业务需求调整即可。
  • 卡片展示:用HeroCard可以让位置信息更清晰,附带的地图跳转按钮能让用户直接查看位置的地图详情,提升交互体验。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:30:54