Доброго времени суток! Хотелось бы узнать как можно сразу ответить на несколько сообщении?
На данный момент:
[10:21:30] Пользователь один: Погода
[10:21:30] Пользователь два: Погода
Ответит только пользователю два.
Код:
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);
}
}
}
Заранее спасибо.