/// <summary> /// Tries to apply rule. If first word in the message matches @match, then action is executed /// </summary> /// <param name="r">response from poll server containing the message</param> public void Apply(PollServerResponse r) { // \u3000 is a Japanese space character if (r.text.Split(new char[] { ' ', '\u3000' })[0] == match) { Console.WriteLine("\nACTION > " + match + "\n"); act(r); } }
private void Initialize(dynamic apiParams, ulong appId, IncomingMessageCallback callback, int exclusivePeerId) { vk = new VkApi(); vk.Authorize(apiParams); var firstResponse = vk.Messages.GetLongPollServer(); //Console.WriteLine("key: " + firstResponse.Key + "\nserver: " + firstResponse.Server + "\nts: " + firstResponse.Ts); Console.WriteLine("Ready."); server = new LongPollServer(firstResponse); if (exclusivePeerId != 0) { this.exclusivePeerId = exclusivePeerId; this.answerAll = false; } server.StartPollingAsync(x => { //get new messages, detailed info on format: https://vk.com/dev/using_longpoll var newMsgs = ((JArray)x["updates"]) .Where(t => (int)t[0] == 4 && (answerAll ? // if it answers all (int)t[3] != vk.UserId : // then answer everybody except yourself ((int)t[3] == vk.UserId || (int)t[3] == exclusivePeerId) // else answer only yourself or exclusive peer ) //TODO: probably logic bug here. needs fix ) .Select(c => c).ToList(); //actually got new messages if (newMsgs.Count != 0) { try { var psr = new PollServerResponse(x); // apply rules one by one looking for one, which applies here rules.ForEach(rule => rule.Apply(psr)); callback(this, psr); } catch (FormatException) { //sometimes it happens. just nevermind :) Console.WriteLine("Known issue. Couldn't parse server response to struct.\n"); } } }); }