Esempio n. 1
0
        internal async Task SetStateAsync(Identity identity, string state, bool raiseEvent)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }
            if (state.Equals(Constants.DEFAULT_STATE, StringComparison.OrdinalIgnoreCase))
            {
                using (var cts = new CancellationTokenSource(CommandTimeout))
                {
                    await _bucket.DeleteAsync(GetKey(identity), cts.Token);
                }
            }
            else
            {
                using (var cts = new CancellationTokenSource(CommandTimeout))
                {
                    await _bucket.SetAsync(GetKey(identity), new StateDocument { State = state }, StateTimeout, cts.Token);
                }
            }

            if (raiseEvent)
            {
                StateChanged?.Invoke(this, new StateEventArgs(identity, state));
            }
        }
        public async Task <bool> HandleAsync(Message message, CancellationToken cancellationToken)
        {
            try
            {
                var action = message.Content.ToString().Trim();
                action = RemoveDevTag(action);


                if (action.Equals("")) //DevAction to delete current user from the bot's context
                {
                    var bucketKey = _contextManager.GetBucketKey(message.From);
                    await _bucket.DeleteAsync(bucketKey);

                    await _sender.SendMessageAsync("DELETION DONE", message.From, cancellationToken);
                }
                else if (action.Equals(""))           //DevAction to refresh scheduled messages. TODO: automate
                {
                    if (message.From.Name.Equals("")) //To make sure no one exploits this, set the Main Dev identifier from facebook here
                    {
                        await _scheduler.UpdateBroadcastMessagesAsync(_owlFilter, _settings);

                        await _sender.SendMessageAsync("UPDATE DONE", message.From, cancellationToken);
                    }
                    else
                    {
                        await _sender.SendMessageAsync("NOT MAIN DEV. ABORTING", message.From, cancellationToken);

                        throw new Exception();
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
 public Task ClearSessionAsync(Node node, CancellationToken cancellationToken)
 => _bucketExtension.DeleteAsync(GetSessionKey(node), cancellationToken);
Esempio n. 4
0
        public async Task ReceiveAsync(Message message, CancellationToken cancellationToken)
        {
            var fromIdentity = message.From.ToIdentity();

            // Gets the stored document for the identity
            var userSession = await _bucketExtension.GetAsync <JsonDocument>(
                fromIdentity,
                cancellationToken);

            var text = message.Content.ToString().ToLowerInvariant();

            // Creates the response
            var responseMessage = new Message()
            {
                Id      = EnvelopeId.NewId(),
                To      = message.From,
                Content = "Pong!!!"
            };

            // If there is a stored session
            if (userSession != null)
            {
                if (text == "ping")
                {
                    // Removes the session
                    await _bucketExtension.DeleteAsync(fromIdentity, cancellationToken);
                }
                else
                {
                    var state = userSession["state"].ToString();
                    switch (state)
                    {
                    case "cachorro":
                        responseMessage.Content = "Au Au Au!!!";
                        break;

                    case "gato":
                        responseMessage.Content = "Miau Miau Miau!!!";
                        break;

                    case "EscolherAnimal":
                        if (new[] { "cachorro", "gato" }.Contains(text))
                        {
                            // Sets the current state as the animal name
                            await _bucketExtension.SetAsync(
                                fromIdentity,
                                new JsonDocument
                            {
                                { "state", text }
                            },
                                cancellationToken : cancellationToken);

                            if (text == "cachorro")
                            {
                                responseMessage.Content =
                                    new MediaLink
                                {
                                    Title = "Cachorro",
                                    Text  = "Agora sou um cachorro",
                                    Type  = "image/jpeg",
                                    Uri   = new Uri("http://tudosobrecachorros.com.br/wp-content/uploads/cachorro-independente-766x483.jpg"),
                                };
                            }
                            else if (text == "gato")
                            {
                                responseMessage.Content =
                                    new MediaLink
                                {
                                    Title = "Gato",
                                    Text  = "Agora sou um gato",
                                    Type  = "image/jpeg",
                                    Uri   = new Uri("http://www.gatosmania.com/Uploads/gatosmania.com/ImagensGrandes/linguagem-corporal-gatos.jpg"),
                                };
                            }
                        }

                        break;
                    }
                }
            }
            else if (text == "animal")
            {
                await _bucketExtension.SetAsync(
                    fromIdentity,
                    new JsonDocument
                {
                    { "state", "EscolherAnimal" }
                },
                    cancellationToken : cancellationToken);

                responseMessage.Content =
                    new Select
                {
                    Text    = "Escolha uma opção",
                    Options = new[]
                    {
                        new SelectOption {
                            Order = 1, Text = "Cachorro"
                        },
                        new SelectOption {
                            Order = 2, Text = "Gato"
                        }
                    }
                };
            }

            await _sender.SendMessageAsync(responseMessage, cancellationToken);
        }