Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Created May 27, 2018 23:56
Show Gist options
  • Save icebeam7/3cf8b5fe8b4440bf73cda07c763a9ad7 to your computer and use it in GitHub Desktop.
Save icebeam7/3cf8b5fe8b4440bf73cda07c763a9ad7 to your computer and use it in GitHub Desktop.
XamarinFaceBot: ServicioBotDirectLine.cs
using XamarinFaceBot.Modelos;
using Microsoft.Bot.Connector.DirectLine;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using XamarinFaceBot.Helpers;
namespace XamarinFaceBot.Servicios
{
public class ServicioBotDirectLine
{
public DirectLineClient ClienteDL;
public Conversation Conversacion;
public ChannelAccount Cuenta;
public ServicioBotDirectLine(string nombre)
{
var tokenResponse = new DirectLineClient(Constantes.DirectLineSecret).Tokens.GenerateTokenForNewConversation();
ClienteDL = new DirectLineClient(tokenResponse.Token);
Conversacion = ClienteDL.Conversations.StartConversation();
Cuenta = new ChannelAccount { Id = nombre, Name = nombre};
}
public void EnviarMensaje(string mensaje)
{
var activity = new Activity
{
From = Cuenta,
Text = mensaje,
Type = ActivityTypes.Message
};
ClienteDL.Conversations.PostActivity(Conversacion.ConversationId, activity);
}
public async Task ObtenerMensajes(ObservableCollection<Mensaje> collection)
{
string watermark = null;
while (true)
{
var set = await ClienteDL.Conversations.GetActivitiesAsync(Conversacion.ConversationId, watermark);
watermark = set?.Watermark;
foreach (var actividad in set.Activities)
{
if (actividad.From.Id == Constantes.BotID)
collection.Add(new Mensaje(actividad.Text, actividad.From.Name));
}
await Task.Delay(3000);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment