why Firebase in C# form app not realtime? - c#

I'm trying to build an app that detects product from android and send it in real-time to the desktop application via firebase but I don't know firebase doesn't work in real-time when I get value on C# app (I mean when I change values it doesn't change in the app automatically.)
Rq: I'm using FireSharp reference and My code for receiving:
public void getdata()
{
var res = client.Get(#"Store/");
Store str = res.ResultAs<Store>();
store.Text = str.Tags;
nb.Text = str.nb;
}

By calling the Get method, you're calling the server to get the data only once.
If you want to instead get the data now and then continue to listen for updates, use the OnAsync method. From the linked documentation:
EventStreamResponse response = await _client.OnAsync("chat", (sender, args, context) => {
System.Console.WriteLine(args.Data);
});
//Call dispose to stop listening for events
response.Dispose();

Related

C# FirebaseDataBase API - Real Time Get Data into a List

I´m new in this topic about Firebase in C#, the online documetation is bad, I only find 2 (FireSharp and FireBaseDataBase) API for connect my Desktop App with Firebase and also I try to connect my app with JSON HttpRequest. In all the case when i try to PUT, UPDATE, DELETE, the response time delay 4 seconds and i´m sure that my internet have a good Connection.
On the other hand and as a main question (using the API FirebaseDataBase for C# WPF) is why i can´t put the real time data in a List(). First I try to do this.
//Example//`
public void StreamData()
{
List<Dinosaur> group= new List<Dinosaur>();
var firebase = new FirebaseClient("https://dinosaur-facts.firebaseio.com/");
var observable = firebase
.Child("dinosaurs")
.AsObservable<Dinosaur>()
.Subscribe(d => group.Add(d.Object));
}
But the problem here is that "group" dont Add "d.Object".
Then I try to use this code:
public void StreamData()
{
List<Persona> g = new List<Persona>();
var observable = firebaseClient.Child("Information").AsObservable<Persona>();
var t= observable.ObserveOnDispatcher().AsObservableCollection();
}
But I have this problem :
System.InvalidOperationException: 'The current thread has no
Dispatcher associated with it.'
In summary i try to get a real time data from Firebase with the API FireBaseDataBase, as a List() because i want to insert in my Datagrid.

Resume Bot Framework dialog when triggered by external service

The Scenario
I have a bot built using the Bot Framework with a series of dialogs. One of these dialogs gives the user the option of inputting some complex data via a web page by presenting a button to them. Clicking the button they are then taken to the site, fill out the data, save and are then directed back to the bot.
I want my bot to pause the dialog until it receives an event from my web page telling me the user has saved the data and then continue asking the user questions.
Before
I had a version implemented whereby I would store a ConversationReference before the user clicked the button and then when the external event happened I would send the cards and next messages I wanted to show (not in a dialog) from a webhook, that was fine but it got quite complicated/messy - I'd rather keep the whole app in one continuous dialog.
Idea 1: Use DirectLine API
I did some research and many people were suggesting using the DirectLine API. So I implemented this:
public async Task SendEventAsync(InternalEventMessage message, ConversationReference reference) {
var client = new DirectLineClient(!String.IsNullOrEmpty(_settings.DirectLineSecret) ? _settings.DirectLineSecret : null);
if (_settings.SiteUrl.Contains("localhost")) {
client.BaseUri = new Uri(_settings.DirectLineServiceUrl);
}
var eventMessage = Activity.CreateEventActivity();
//Wrong way round?!?
eventMessage.From = reference.Bot;
eventMessage.Type = ActivityTypes.Event;
eventMessage.Value = message;
var conversation = await client.Conversations.PostActivityAsync(reference.Conversation.Id, eventMessage as Activity);
}
This uses the DirectLine client to send an event message to the serviceUrl using a stored ConversationReference, basically imitating a user (bot and user seem to be the wrong way round in the SDK). Checking for localhost was so that the DirectLine library pointed at the emulator server rather than https://directline.botframework.com.
In my dialog I call:
//method above shows input button and links to web page
context.Wait(WaitForAddressInput);
}
private async Task WaitForAddressInput(IDialogContext context, IAwaitable<IActivity> result) {
var message = await result;
switch (message.Type) {
case ActivityTypes.Message:
//TODO: Add response
break;
case ActivityTypes.Event:
var eventMessage = message as IEventActivity;
if (((JObject)eventMessage.Value).ToObject<InternalEventMessage>().Type == EventType.AddressInputComplete) {
_addressResult = (await _tableService.ReadOrderById(Order.OrderId)).Address;
await context.PostAsync($"Great}");
context.Done(_addressResult);
}
break;
}
}
This waits for any message from the user after the button has been shown and if our event matches then we proceed with the dialog.
This works locally using the emulator but, frustratingly, doesn't live. It fails to recognise channels created via webchat or Messenger. That is explained here: Microsoft Bot Framework DirectLine Can't Access Conversations
For security reasons, you can't use DirectLine to spy on messages from
another conversation.
So I can't access a channel that I haven't created using DirectLine.
Idea 2: BotConnector
So I thought I'd try the BotConnector using similar code:
public async Task SendEventAsync(InternalEventMessage message, Microsoft.Bot.Connector.DirectLine.ConversationReference reference) {
var botAccount = new ChannelAccount(reference.User.Id, reference.User.Name);
var userAccount = new ChannelAccount(reference.Bot.Id, reference.Bot.Name);
MicrosoftAppCredentials.TrustServiceUrl(reference.ServiceUrl);
var connector = new ConnectorClient(new Uri(reference.ServiceUrl), new MicrosoftAppCredentials("xxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxx"));
connector.Credentials.InitializeServiceClient();
var eventMessage = Activity.CreateMessageActivity();
eventMessage.Recipient = botAccount;
eventMessage.From = userAccount;
eventMessage.Type = ActivityTypes.Event;
eventMessage.Conversation = new ConversationAccount(id: reference.Conversation.Id);
eventMessage.ServiceUrl = reference.ServiceUrl;
eventMessage.Timestamp = DateTimeOffset.UtcNow;
eventMessage.LocalTimestamp = DateTime.Now;
eventMessage.ChannelId = reference.ChannelId;
var result = await connector.Conversations.SendToConversationAsync(eventMessage as Microsoft.Bot.Connector.Activity);
}
This doesn't crash and I can see the event appear in the emulator request console but nothing happens, it seems to be ignored!
Idea 3: Try to imitate the bot service calling my bot
I haven't tried this yet because I think it might be the most time consuming but I was reading here about the service authentication and wondered if it would be possible to imitate the hosted bot service sending a message and send my event that way with the required data?
This seems like a fairly common scenario so I'm surprised I haven't come across a way to do this yet. If anyone has any other ideas on how I can send an event message to my bot from an external service then I'd love to hear it.
Update:
See my answer below Eric's to see what I did.
Idea 1:
DirectLine is a channel, not a library to use in order to connect to channels. (For instance: you would not use Facebook Messenger to connect to Skype) DirectLineClient is useful for creating a client application that connects to the DirectLine channel through the Direct Line connector service.
Idea 2:
This method should work. In fact, the BotAuth library uses this method for the MagicNumber login flow within the CallbackController: https://github.com/MicrosoftDX/botauth/blob/9a0a9f1b665f4aa95b6d60d09346dda90d8b314e/CSharp/BotAuth/Controllers/CallbackController.cs
For your scenario, you should be able to construct a CardAction of type ActionTypes.OpenUrl that contains a value with the ConversationReference encoded in the url. Clicking the button will call an mvc controller that displays a page (saving the ConversationReference in a cookie or something) and when the user finishes adding the address on the page, use the ConversationReference to send an event to the bot (similar to how BotAuth resumes the conversation in the CallbackController).
Idea 3:
This would bypass the connector services, and is not a supported scenario. The link you shared explains the details of how authentication works in the Bot Framework, not how to bypass the connector services.
Eric's answer led me to solve the issue using the BotAuth example but, for completeness, here is what I did using Idea 2.
I created a CallbackController on my Bot Framework endpoint and then used the following code to send an event back to the awaiting dialog:
MicrosoftAppCredentials.TrustServiceUrl(reference.ServiceUrl);
var message = reference.GetPostToBotMessage();
message.Value = new InternalEventMessage(type);
message.Type = ActivityTypes.Event;
await Conversation.ResumeAsync(reference, message);
The dialog awaits with this code and continues:
context.Wait(WaitForAddressInput);
}
private async Task WaitForAddressInput(IDialogContext context,
IAwaitable<IActivity> result)
{
var message = await result;
switch (message.Type)
{
case ActivityTypes.Message:
//TODO: Add response
break;
case ActivityTypes.Event:
//Process event and continue!
break;
}
}
This is the most complicated issue I've had with the Bot Framework and I found the docs a little lacking. Hope this helps someone!

Calling IInteractionVoice.Release() not releasing (hanging up) the current call in a Workspace Desktop Edition custom module

I have a requirement to hang up the current phone call programmatically in Genesys Workspace Desktop edition. Here is what I have:
public class SomeService
{
private readonly IEnterpriseServiceProvider _esp;
public SomeService(IEnterpriseServiceProvider esp)
{
_esp = esp;
}
public void HangupCurrentCall()
{
var iv = _esp.Resolve<IInteractionVoice>();
iv.Release();
}
}
The code above is executing with no error, but the call is not being hung up.
You can't hangup current call just from enterprise service. WDE providing API for that. You can check from developer guide document. Actually you have two options to achieve this. First way using WDE API command calls. Second way using universal SDK (PSDK) to hangup current call.
First of all you need to collect current call's interactionId. After that can call a command like that,
commandManager.CommandsByName["InteractionVoiceReleaseCall"].Insert(0, new CommandActivator()
{
CommandType = typeof(CustomCommand.ReleaseCall),
Name = "InteractionVoiceReleaseCall"
});
You can find all commands list from WDE api guide.
On your command type (class) you must return boolean. If you return false, its ok to continue, sending true like break the command.
Or you can directly execute a command like this;
IDictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("CommandParameter", interaction);
parameters.Add("Reasons", reasons);
parameters.Add("Extensions", extensions);
commandManager.GetChainOfCommandByName("InteractionVoiceReleaseCall").Execute();
As a SDK certificated developer, i always prefer PSDK(universal genesys sdk). You can retrieve current SIP Server connection and send request to it. Like this code block
IChannelService channelService = agent.EntrepriseService.Resolve<IChannelService>("channelService");
IClientChannel tServerChannel = channelService.ListChannels<Genesyslab.Platform.Voice.Protocols.TServerProtocol>().FirstOrDefault();
channelService.RegisterEvents(tServerChannel, new Action<Genesyslab.Enterprise.Model.Channel.IClientChannel>(ChannelEvent));
TServerProtocol tServerProtocol = tServerChannel.Protocol as TServerProtocol;
After this you have current connection on tserverPorotocol object. Then you can send a request to SIP Server.
Like this:
Genesyslab.Platform.Voice.Protocols.TServer.Requests.Voice.RequestReleaseCall releaseCall = Genesyslab.Platform.Voice.Protocols.TServer.Requests.Voice.RequestReleaseCall.Create();
releaseCall.ThisDN = "7000"; //(for example)(you can retrieve agent's DN from agent object)
releaseCall.ConnID = interaction.ConnectionId // you can retrieve from interactionhandler event.
tServerProtocol.Send(releaseCall);
//or tServerProtocol.Request(releaseCall); for async request. request return a ack message from the server.
I tried to explain basics. I hope its helpful. If you got a question about sip or etc. please let me know.

Android - Refit throws UnknownHostException unexpectedly

I'm trying to implement Refit using Xamarin and for some reason it throws a UnknownHostException (UHExc) if I was previously not connected to wifi while the app was open. This doesn't happen consistently though.
I have these two calls to Refit's instantiation of my "Refit-interface": PostLoginAsync and GetDataAsync, as shown below (the guide I've been following is here):
public async Task<SomeClass> PostLogin(string user, string password)
{
SomeClass response = null;
var loginTask = apiService.UserInitiated.PostLoginAsync(new RequestBody(user: user, password: password));
response = await FireWebTask(loginTask);
return response;
}
and
private async Task<List<Data>> GetRemoteDataAsync(string args)
{
List<Data> list = null;
var getDataTask = apiService.UserInitiated.GetDataAsync(args);
list = await FireWebTask(getDataTask);
return list;
}
The "Refit-interface" looks somewhat like this:
...
[Post("/relative/url/to/login")]
Task<SomeClass> PostLoginAsync([Body(BodySerializationMethod.Json)] RequestBody requestBody);
[Get("/relative/url/to/data")]
Task<List<Data>> GetDataAsync([Header("SomeHeader")] string args);
...
When I open the app with no connection to the internet and try to send the PostLogin-request, I get an UHExc as expected. If I then turn on the wifi and try again (without closing the app) I get the UHExc again, only this time with almost no delay as the first time (as if the exception was cached??). Restarting the app and trying again without disconnecting the wifi works fine.
If I do the exact same thing with the second request (GetData) I first get an UHExc (obviously) but when reconnecting the wifi it works flawlessly. So it seems to me like the POST-request caches the exception or something and throws it repeatedly without trying to connect at all. How can I solve this (whatever the problem actually is)?
I also had this problem and after some testing discovered that the issue is with the Fusillade library. Now the initial problem is that the fixing changes are not reflected in the NuGet packages so you need to download the latest source from the Github repo and reference the newer dlls.
Looks like the underlying issue is due to the failed requests getting enqueued and played back even though the request resulted in a WebException.
I included the latest code into my project and confirmed that they are working.

SignalR .NET Client not receiving messages

I have a .NET console application that I am practicing signalR with.
var hubConnection = new HubConnection("http://URL/signalr/");
var hub = hubConnection.CreateHubProxy("Hub");
hub.StateChanged += change =>
{
Console.WriteLine(change.NewState);
};
hub.Received += s =>
{
Console.WriteLine(s);
};
hub.On<string, string>("processMessage", (group, message) =>
{
Console.WriteLine(message);
});
await hubConnection.Start();
await hub.Invoke<string>("Subscribe", "New group");
I see the state changing from Connecting to Connected but I am not getting a "Received" event on the client when the server sends a message. The server is sending a group message as soon as the client subscribes and I can see the message being sent with the correct "New group" groupname, however I never receive the message on the client. I also do not receive the processMessage event when the server uses that method.
Server Code
private void CallBack(string group, string message)
{
Clients.Group(group).processMessage(group, message);
}
The other method on the server is Subscribe which just sets my inner server to use the CallBack method when it has data available to send to the client.
Edit
This works in Javascript it just doesn't seem to work in the .NET client.
Without full serverside code it's hard to say but I think this part is wrong
hubConnection.CreateHubProxy("Hub");
as argument you need the name of your hubclass on serverside. For example
hubConnection.CreateHubProxy("MyHub");
To get more informations on clientside why it fails you can temporary add the following to your HubConnection
hubConnection.TraceLevel = TraceLevels.All;
hubConnection.TraceWriter = Console.Out;
After adding this you will get further debuging informations in your output section in VS

Categories