Quantcast
Channel: かずきのBlog@hatena
Viewing all articles
Browse latest Browse all 1387

Xamarin.FormsでBotFrameworkのDirectLineを試す

$
0
0

自分用メモです。(雑です)

PCLを辞めます。 .NET Standardにプロジェクトを変換します。

以下をプロジェクトに追加する。

  • Micrsoft.Bot.Connector.DirectrLine
  • Microsoft.Rest.ClientRuntime

こんな感じに書けば動いた!とりあえず

<?xml version="1.0" encoding="utf-8"?><ContentPage xmlns="http://xamarin.com/schemas/2014/forms"xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"prism:ViewModelLocator.AutowireViewModel="True"x:Class="PrismUnityApp4.Views.MainPage"Title="MainPage"><StackLayout HorizontalOptions="Center"VerticalOptions="Center"><Button Text="Send"Command="{Binding SendCommand}" /><Button Text="NewConversation"Command="{Binding NewConversationCommand}" /><Label Text="{Binding Message}" /></StackLayout></ContentPage>

C#

using Microsoft.Bot.Connector.DirectLine;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using System.Linq;

namespace PrismUnityApp4.ViewModels
{
    publicclass MainPageViewModel : BindableBase, INavigationAware
    {
        privatestaticreadonlystring Secret = "DirectLine の シークレット";
        privatestaticreadonlystring Id = "okazuki";

        private Conversation Conversation { get; set; }

        public DelegateCommand SendCommand { get; }

        public DelegateCommand NewConversationCommand { get; }

        privatestring message;

        publicstring Message
        {
            get { returnthis.message; }
            set { this.SetProperty(refthis.message, value); }
        }

        public MainPageViewModel()
        {
            this.SendCommand = new DelegateCommand(Send);
            this.NewConversationCommand = new DelegateCommand(() => this.Conversation = null);
        }

        private async void Send()
        {
            var client = new DirectLineClient(Secret);
            if (this.Conversation == null)
            {
                this.Conversation = await client.Conversations.StartConversationAsync();
            }

            var activity = Activity.CreateMessageActivity() as Activity;
            activity.From = new ChannelAccount { Id = Id };
            activity.Text = "Hello world";
            var response = await client.Conversations.PostActivityAsync(this.Conversation.ConversationId, activity);
            var activities = await client.Conversations.GetActivitiesAsync(this.Conversation.ConversationId);
            var result = activities.Activities.FirstOrDefault(x => x.ReplyToId == response.Id);

            this.Message = result?.Text;
        }

        publicvoid OnNavigatedFrom(NavigationParameters parameters)
        {

        }

        publicvoid OnNavigatedTo(NavigationParameters parameters)
        {
        }
    }
}

Viewing all articles
Browse latest Browse all 1387

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>