Beispiel #1
0
		public async Task<Message> SendMessage (Message message)
		{
			using (var client = new HttpClient ()) {

				client.DefaultRequestHeaders.Accept.Clear ();
				client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue ("Basic", GetBase64CredentialString ());

				var param = new List<KeyValuePair<string,string>> ();
				param.Add (new KeyValuePair<string, string> ("input", message.Text));
				if (App.ConversationId != 0) {
					param.Add (new KeyValuePair<string, string> ("conversation_id", App.ConversationId.ToString ()));
					param.Add (new KeyValuePair<string, string> ("client_id", App.ConversationId.ToString ()));
				}

				var data = await client.PostAsync (string.Format (App.DialogUrl, App.DialogId), new FormUrlEncodedContent (param)).ConfigureAwait (false);
				return await HandleResponse (data);
			}
		}
Beispiel #2
0
		public async Task SendMessage (string message = null)
		{
			Message messageItem;
			if (!string.IsNullOrEmpty (message)) {
				messageItem = new Message {
					IsIncoming = false,
					Text = message,
					Status = MessageStatus.Sent,
					Timestamp = DateTime.UtcNow,
				};
				await App.DataManager.SaveMessage (messageItem);
				Messages.Add (messageItem);

			} else
				messageItem = new Message{ Text = "Hello!" };

			var incomingMessage = await dialogService.SendMessage (messageItem);
			await App.DataManager.SaveMessage (incomingMessage);
			messageItem.Status = MessageStatus.Delivered;
			Messages.Add (incomingMessage);

		}
Beispiel #3
0
		public Task<int> SaveMessage(Message message)
		{
			return db.SaveItemAsync<Message> (message);
		}
Beispiel #4
0
		private async Task<Message> HandleResponse (HttpResponseMessage response)
		{
			var data = await response.Content.ReadAsStringAsync ().ConfigureAwait (false);
			return await Task.Run (() => {
				var watsonData = JsonConvert.DeserializeObject<WatsonResponse> (data);
				if (watsonData == null) {
					return null;
				}
				App.ClientId = watsonData.ClientId;
				App.ConversationId = watsonData.ConversationId;
				StringBuilder msgBuilder = new StringBuilder ();
				foreach (var item in watsonData.Response) {
					msgBuilder.AppendLine (item);
				}

				var message = new Message {
					Text = msgBuilder.ToString (),
					Timestamp = DateTime.UtcNow,
					IsIncoming = true,
					Status = MessageStatus.Delivered
				};


				return message;
			});

		}