Exemplo n.º 1
0
        public void Notify <T>(T message, MessengerEvent context)
        {
            Console.WriteLine("Notify requested for " + context);

            IEnumerable <KeyValuePair <MessengerKey, object> > result;

            if (context == null)
            {
                // Get all recipients where the context is null.
                result = from r in Dictionary where r.Key.Context == null orderby r.Key.ID select r;
            }
            else
            {
                // Get all recipients where the context is matching.
                result = from r in Dictionary
                         where r.Key.Context != null && r.Key.Context.Equals(context)
                         orderby r.Key.ID
                         select r;
            }

            foreach (var action in result.Select(x => x.Value))
            {
                // Send the message to all recipients.
                if (action.GetType() == typeof(Action <T>))
                {
                    ((Action <T>)action)(message);
                    Console.WriteLine("Sending broadcast " + ((Action <T>)action).Method.Name);
                }
                else if (action.GetType() == typeof(Action))
                {
                    ((Action)action)();
                    Console.WriteLine("Sending broadcast " + ((Action)action).Method.Name);
                }
            }
        }
Exemplo n.º 2
0
        public void Unsubscribe(object subscriber, MessengerEvent msgEvent)
        {
            object       action;
            MessengerKey key = GetMessengerKey(subscriber, msgEvent);


            if (subscriber != null)
            {
                if (key != null)
                {
                    Dictionary.TryRemove(key, out action);
                }
            }
        }
Exemplo n.º 3
0
 public void Accept(string eventName, UnityAction <object[]> eventListener)
 {
     lock (m_MessengerLock)
     {
         if (m_EventDictionary.TryGetValue(eventName, out MessengerEvent thisEvent))
         {
             thisEvent.AddListener(eventListener);
         }
         else
         {
             thisEvent = new MessengerEvent();
             thisEvent.AddListener(eventListener);
             m_EventDictionary.Add(eventName, thisEvent);
         }
     }
 }
Exemplo n.º 4
0
        private MessengerKey GetMessengerKey(object subscriber, MessengerEvent msgEvent)
        {
            MessengerKey item = null;

            try
            {
                foreach (var key in Dictionary.Keys)
                {
                    if (subscriber == key.Subscriber && msgEvent == (MessengerEvent)key.Context)
                    {
                        item = key;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(item);
        }
Exemplo n.º 5
0
        public async Task <HttpStatusCode> RecieveAsync()
        {
            // var xt = graph.GetPageProfileAsync(PSID).Result;

            string request = "";

            //  GraphApi graph = new GraphApi();
            using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8, true, 1024, true))
            {
                request = await reader.ReadToEndAsync();
            }
            var messengerEvent = MessengerEvent.FromJson(request);
            var PSID           = messengerEvent.Entry[0].Messaging[0].Sender.Id;

            if (messengerEvent.Entry[0].Messaging[0].Postback != null)
            {
                if (messengerEvent.Entry[0].Messaging[0].Postback.Payload == "GET_STARTED" || messengerEvent.Entry[0].Messaging[0].Postback.Payload == "RESTART_BOT")
                {
                    try
                    {
                        this._queue.QueueAsyncTask(() => graph.SendHelloAsync(PSID));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
                else if (messengerEvent.Entry[0].Messaging[0].Postback.Payload == "RESTART_QUIZ")
                {
                    this._queue.QueueAsyncTask(() => graph.ProfileInfoHandler(PSID));
                    this._queue.QueueTask(() => graph.FinishScorecard(PSID));
                    //this._queue.QueueTask(() => graph.Typing(PSID));
                    this._queue.QueueTask(() => graph.CreateScorecard(PSID));
                    this._queue.QueueTask(() => graph.SetState(PSID, 0, Startup.BotContext.Quiz.ToString()));
                    this._queue.QueueTask(() => graph.SendMessage((GenericMessage($"Alright.\nThere will be {Startup.quiz.Questions.Count} questions.\nSome questions have multiple answers.", PSID).ToJson())));

                    this._queue.QueueTask(() => Thread.Sleep(300));

                    this._queue.QueueTask(() => graph.SendMessage((GenericMessage("You may either tap or type in your answers. When typing in answers, options should be separated by a space or a comma, such as \"A, B\" or \"A B\", but not \"AB\".", PSID).ToJson())));

                    this._queue.QueueTask(() => Thread.Sleep(300));

                    this._queue.QueueTask(() => graph.SendMessage((GenericMessage("First Question for you", PSID).ToJson())));

                    this._queue.QueueTask(() => Thread.Sleep(300));

                    this._queue.QueueTask(() => NextQuestionAsync(PSID, 0, Startup.BotContext.Quiz.ToString()));
                }
            }
            else if (messengerEvent.Entry[0].Messaging[0].Message.Text == null)
            {
                this._queue.QueueTask(() =>
                                      new Dialogflow().DetectIntentFromTexts(PSID, Startup.Force_Wrong));
            }
            else
            {
                //this._queue.QueueTask(() =>graph.Typing(PSID));
                this._queue.QueueTask(() =>
                                      new Dialogflow().DetectIntentFromTexts(PSID, messengerEvent.Entry[0].Messaging[0].Message.Text));
            }
            return(HttpStatusCode.OK);
        }
Exemplo n.º 6
0
 public void RaiseMessengerEvent(bool result)
 {
     MessengerEvent?.Invoke(this, new MessengerEventArgs(result));
 }
Exemplo n.º 7
0
        public void Subscribe <T>(object subscriber, MessengerEvent mgsEvent, Action <T> handler)
        {
            var key = new MessengerKey(subscriber, mgsEvent, Dictionary.Count);

            Dictionary.TryAdd(key, handler);
        }
Exemplo n.º 8
0
 public MessengerKey(object subscriber, MessengerEvent context, int id)
 {
     this.Subscriber = subscriber;
     this.Context    = context;
     this.ID         = id;
 }