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); } } }
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); } } }
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); } } }
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); }
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); }
public void RaiseMessengerEvent(bool result) { MessengerEvent?.Invoke(this, new MessengerEventArgs(result)); }
public void Subscribe <T>(object subscriber, MessengerEvent mgsEvent, Action <T> handler) { var key = new MessengerKey(subscriber, mgsEvent, Dictionary.Count); Dictionary.TryAdd(key, handler); }
public MessengerKey(object subscriber, MessengerEvent context, int id) { this.Subscriber = subscriber; this.Context = context; this.ID = id; }