C
C
continue2015-11-01 12:49:57
C++ / C#
continue, 2015-11-01 12:49:57

Reply immediately to multiple messages | VK API | VK BOTS - C#?

Good day! I would like to know how you can reply to several messages at once?
Currently:
[10:21:30] User one: Weather
[10:21:30] User two: Weather
Will only reply to user two.
The code:

namespace ConsoleApplicationTest
{
    public static class Program
    {
        private static System.Timers.Timer aTimer;
        public static void Main()
        {
            aTimer = new System.Timers.Timer(3000);
            aTimer.Enabled = true;
            aTimer.Elapsed += OnTimedEvent;
            Console.ReadLine();
        }
        private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
        {
            var request = (HttpWebRequest)WebRequest.Create("https://api.vk.com/method/messages.get?");
            request.Method = "post";
 
            string data = "count=1&access_token=токен&expires_in=0&user_id=123663731";
            request.ContentLength = data.Length;
 
            IAsyncResult getRequestStream = request.BeginGetRequestStream(null, null);
            var writer = new StreamWriter(request.EndGetRequestStream(getRequestStream));
            writer.Write(data);
            writer.Close();
 
            Console.WriteLine("Посылаем запрос");
            request.BeginGetResponse(OnAsyncCallback, request);
            Console.WriteLine("Запрос послан");
 
            Console.ReadKey();
        }
 
        private static void OnAsyncCallback(IAsyncResult asyncResult)
        {
            var httpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
            WebResponse response = httpWebRequest.EndGetResponse(asyncResult);
            Console.WriteLine("ответ получен");
            var reader = new StreamReader(response.GetResponseStream());
            string str = reader.ReadToEnd();
            Console.WriteLine(str);
            JObject testObject = JObject.Parse(str);
            string body = "";
            int id_vk = -1;
            int mid = -1;
            foreach (JToken j in testObject["response"])
            {
                if (j is JObject)
                {
                    body = (string)j["body"];
                    id_vk = (int)j["uid"];
                    mid = (int)j["mid"];
                }
            }
            if (body.Contains("погод"))
            {
                var request = (HttpWebRequest)WebRequest.Create("https://api.vk.com/method/messages.send?");
                request.Method = "post";
                string data = "user_id=" + id_vk + "&access_token=токен&expires_in=0&guid=" + mid + "&message=messenge api vk | C# | Test"; 
                IAsyncResult getRequestStream = request.BeginGetRequestStream(null, null);
                var writer = new StreamWriter(request.EndGetRequestStream(getRequestStream));
                writer.Write(data);
                writer.Close();
                Console.WriteLine("Посылаем запрос");
                request.BeginGetResponse(OnAsyncCallbackTwo, request);
                Console.WriteLine("Запрос послан");
            }
            Console.WriteLine(id_vk);
            Console.WriteLine(body);
        }
        private static void OnAsyncCallbackTwo(IAsyncResult asyncResult)
        {
            var httpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
            WebResponse response = httpWebRequest.EndGetResponse(asyncResult);
            var reader = new StreamReader(response.GetResponseStream());
            string str = reader.ReadToEnd();
            Console.WriteLine("ответ получен");
            Console.WriteLine(str);
        }
    }
}

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ar4ebaldello, 2015-11-01
@ar4ebaldello

If your bot needs to listen to all messages, then you need a LongPoll

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question