public async Task <HttpResponseMessage> SendMessage()
        {
            try
            {
                if (!string.IsNullOrEmpty(ConversationStarter.fromId))
                {
                    await ConversationStarter.Resume(ConversationStarter.conversationId, ConversationStarter.channelId); //We don't need to wait for this, just want to start the interruption here

                    var resp = new HttpResponseMessage(HttpStatusCode.OK);
                    resp.Content = new StringContent($"<html><body><h1>Nice Work!</h1><a href=\"http://*****:*****@"text/html");

                    return(resp);
                }
                else
                {
                    var resp = new HttpResponseMessage(HttpStatusCode.OK);
                    resp.Content = new StringContent($"<html><body>You need to talk to the bot first so it can capture your details.</body></html>", System.Text.Encoding.UTF8, @"text/html");
                    return(resp);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Example #2
0
 public void TimerEvent(object target)
 {
     Contador.Count++;
     if (Contador.Count == 5)
     {
         ConversationStarter.Resume();
     }
 }
        private async void timerEvent(object target)
        {
            t.Dispose();
            await ConversationStarter.Resume(ConversationStarter.conversationId,
                                             ConversationStarter
                                             .channelId);

            t = new Timer(new TimerCallback(timerEvent), null, 1000 * 60 * 5, Timeout.Infinite);
        }
Example #4
0
        /// <summary>
        /// Notify the user with the Dialog
        /// </summary>
        private async Task Notify(IDialog <object> dialog, string userId, string channelId, string serviceUri, int lcid)
        {
            await ConversationStarter.Resume(dialog, userId, channelId, serviceUri);

            if (channelId.ToLower() == "directline")
            {
                var url    = HttpContext.Current.Request.Url;
                var webUrl = $"{url.Scheme}://{url.Host}:{url.Port}/api/LineMessages/Notify?mids={userId}&lcid={lcid}";
                using (HttpClient client = new HttpClient())
                {
                    await client.PostAsync(webUrl, null);
                }
            }
        }
Example #5
0
 /// <summary>
 /// Notify the user with message
 /// </summary>
 private async Task Notify(string message, string userId, string channelId, string serviceUri, int lcid)
 {
     // As directline doesn't support push, directly call push for LINE channel
     if (channelId.ToLower() == "directline")
     {
         var url    = HttpContext.Current.Request.Url;
         var webUrl = $"{url.Scheme}://{url.Host}:{url.Port}/api/LineMessages/Notify?message={message}&mids={userId}&lcid={lcid}";
         using (HttpClient client = new HttpClient())
         {
             await client.PostAsync(webUrl, null);
         }
     }
     else
     {
         await ConversationStarter.Resume(message, userId, channelId, serviceUri);
     }
 }
Example #6
0
        public async Task <HttpResponseMessage> SendMessage()
        {
            try
            {
                if (!string.IsNullOrEmpty(ConversationStarter.resumptionCookie))
                {
                    await ConversationStarter.Resume(); //We don't need to wait for this, just want to start the interruption here

                    var resp = new HttpResponseMessage(HttpStatusCode.OK);
                    resp.Content = new StringContent($"<html><body>Message sent, thanks.</body></html>", System.Text.Encoding.UTF8, @"text/html");
                    return(resp);
                }
                else
                {
                    var resp = new HttpResponseMessage(HttpStatusCode.OK);
                    resp.Content = new StringContent($"<html><body>You need to talk to the bot first so it can capture your details.</body></html>", System.Text.Encoding.UTF8, @"text/html");
                    return(resp);
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Example #7
0
 public void timerEvent(object target)
 {
     t.Dispose();
     ConversationStarter.Resume(ConversationStarter.conversationId, ConversationStarter.channelId); //We don't need to wait for this, just want to start the interruption here
 }
Example #8
0
        private void TimerEventAsync(object target)
        {
            //remove previously created timers
            var ret = ConversationStarter.Timers[me.conversationId + me.channelId];

            if (ret != tAlert.GetHashCode())
            {
                tAlert.Dispose();
                return;
            }

            if (StartAlert.Add(alert.MaxTime) <= DateTimeOffset.Now)
            {
                tAlert.Dispose();
                ConversationStarter.EndAlerts(me.conversationId, me.channelId);
            }
            if (NumberAlerts > AlertMaxNumber)
            {
                tAlert.Dispose();
                ConversationStarter.EndAlertsMax(me.conversationId, me.channelId);
            }
            Electricity res         = null;
            float       consumption = 0;

            switch (alert.AlertType)
            {
            case AlertEnum.Instant:
                var t = myWivaldy.GetMeasures(DateTimeOffset.Now.Add(-alert.Interval), DateTimeOffset.Now);
                t.Wait();
                res = t.Result;
                if (res != null)
                {
                    foreach (var wat in res.Consumptions)
                    {
                        if (wat.watts >= alert.Threshold)
                        {
                            if (consumption < wat.watts)
                            {
                                consumption = wat.watts;
                            }
                        }
                    }
                }
                break;

            case AlertEnum.Total:
                DateTimeOffset today = new DateTimeOffset(DateTimeOffset.Now.Year, DateTimeOffset.Now.Month, DateTimeOffset.Now.Day, 0, 0, 0, DateTimeOffset.Now.Offset);
                var            t1    = myWivaldy.GetDayMeasures(today);
                t1.Wait();
                res = t1.Result;
                if (res != null)
                {
                    var cons = GetKiloWattHour(res);
                    if (cons >= alert.Threshold)
                    {
                        consumption = (float)cons;
                    }
                }
                break;

            case AlertEnum.Switch:
                var cmd = myWivaldy.GetRemoteCommand();
                cmd.Wait();
                RemoteCommand rmc = cmd.Result;
                if (rmc != null)
                {
                    if (rmc.date.millis > remoteCommand.date.millis)
                    {
                        remoteCommand = rmc;
                        //raise an alert
                        ConversationStarter.ResumeCommand(me.conversationId, me.channelId, rmc.commandType, alert);
                    }
                }

                break;

            default:
                break;
            }
            if (consumption > 0)
            {
                NumberAlerts++;
                ConversationStarter.Resume(me.conversationId, me.channelId, consumption, alert);
            }
        }
Example #9
0
        public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> item)
        {
            var message = await item;

            if (message.Text == "logon")
            {
                if (string.IsNullOrEmpty(await context.GetAccessToken(AuthSettings.Scopes)))
                {
                    await context.Forward(new AzureAuthDialog(AuthSettings.Scopes), this.ResumeAfterAuth, message, CancellationToken.None);
                }
                else
                {
                    await TokenSample(context);
                }
            }
            else if (message.Text == "echo")
            {
                await context.PostAsync("echo");

                context.Wait(this.MessageReceivedAsync);
            }
            else if (message.Text == "token")
            {
                await TokenSample(context);
            }
            else if (message.Text.StartsWith("NDBDATA"))
            {
                var messageinfo = message.Text.Split(';');

                try
                {
                    var body = messageinfo[1];
                    Trace.TraceInformation($"ActionDialog: parsing {body}.");
                    var messageDictionary = new JavaScriptSerializer().Deserialize <Dictionary <string, string> >(body);
                    var conversationId    = messageDictionary["conversationId"];
                    var channelId         = messageDictionary["channelId"];
                    var recipientId       = messageDictionary["recipientId"];
                    var recipientName     = messageDictionary["recipientName"];
                    var serviceUrl        = messageDictionary["serviceUrl"];
                    var token             = messageDictionary["token"];

                    Trace.TraceInformation($"ActionDialog: Sending notification to {recipientName}.");
                    await ConversationStarter.Resume(conversationId, channelId, recipientId, recipientName, message.Recipient.Id, message.Recipient.Name, serviceUrl, token);

                    Trace.TraceInformation($"ActionDialog: Notification to {recipientName}.");
                }
                catch (Exception e)
                {
                    Trace.TraceError($"ActionDialog: Error parsing NDBDATA. Exception={e.Message}");
                }

                context.Wait(this.MessageReceivedAsync);
            }
            else if (message.Text == "logout")
            {
                await context.Logout();

                context.Wait(this.MessageReceivedAsync);
            }
            else
            {
                context.Wait(MessageReceivedAsync);
            }
        }
Example #10
0
        private void TimerEventAsync(object target)
        {
            //remove previously created timers
            var ret = ConversationStarter.Timers[me.conversationId + me.channelId];

            if (ret != tAlert.GetHashCode())
            {
                tAlert.Dispose();
                return;
            }

            if (StartAlert.Add(alert.MaxTime) <= DateTimeOffset.Now)
            {
                tAlert.Dispose();
                ConversationStarter.EndAlerts(me.conversationId, me.channelId);
            }
            if (NumberAlerts > AlertMaxNumber)
            {
                tAlert.Dispose();
                ConversationStarter.EndAlertsMax(me.conversationId, me.channelId);
            }
            Electricity res         = null;
            float       consumption = 0;

            if (alert.IsInstant)
            {
                var t = myWivaldy.GetMeasures(DateTimeOffset.Now.Add(-alert.Interval), DateTimeOffset.Now);
                t.Wait();
                res = t.Result;
                if (res != null)
                {
                    foreach (var wat in res.Consumptions)
                    {
                        if (wat.watts >= alert.Threshold)
                        {
                            if (consumption < wat.watts)
                            {
                                consumption = wat.watts;
                            }
                        }
                    }
                }
            }
            else
            {
                DateTimeOffset today = new DateTimeOffset(DateTimeOffset.Now.Year, DateTimeOffset.Now.Month, DateTimeOffset.Now.Day, 0, 0, 0, DateTimeOffset.Now.Offset);
                var            t     = myWivaldy.GetDayMeasures(today);
                t.Wait();
                res = t.Result;
                if (res != null)
                {
                    var cons = GetKiloWattHour(res);
                    if (cons >= alert.Threshold)
                    {
                        consumption = (float)cons;
                    }
                }
            }
            if (consumption > 0)
            {
                NumberAlerts++;
                ConversationStarter.Resume(me.conversationId, me.channelId, consumption, alert);
            }
        }