How to process incoming messages from the user?

Did the bot. Every second send requests to the bot, getting the updates (https://api.telegram.org/bot TOKEN/getUpdates?offset=-1), namely the last message sent by the user.
I send the bot some text, he examines it and displays the corresponding message.
The problem is that the message sent by the bot repeats indefinitely. How to make bot, I sent a message, and he told me only once answer.

Code that sends requests
public void Listen(object state)
{
Bot.GetUpdates();
 Bot.Update = JsonConvert.DeserializeObject<telegramupdates>(Bot.GetResponse());

 if (Bot.Update.result[0].message.text == "/hello") Bot.SendMessage("Test message");
 }</telegramupdates>

The class with the bot
class TelegramBot
{
 private string Token;
 private byte[] Buffer = new byte[8192];

 private HttpWebResponse Response;
 private Stream ResponseStream;

 public TelegramUpdates Update.


 ResposeString StringBuilder= new StringBuilder();


 public TelegramBot(string InputToken)
{
 Token = InputToken; 
}

 public void SendMessage(string InputMessage)
{

 WebRequest Request = (HttpWebRequest)WebRequest.Create("https://api.telegram.org/bot" + Token + "/sendMessage?chat_id=228451619&text="+InputMessage+"");
 Response = (HttpWebResponse)Request.GetResponse();
Request.Abort();

}

 public string GetResponse()
{
ResposeString.Clear();
 ResponseStream = Response.GetResponseStream();

 string tempString = null;
 int count = 0;


do
{

 count = ResponseStream.Read(Buffer, 0, Buffer.Length);
 if (count != 0)
{
 tempString = Encoding.ASCII.GetString(Buffer, 0, count);
ResposeString.Append(tempString);
}
 } while (count > 0);


 return ResposeString.ToString();
}

 public void GetUpdates()
{
 WebRequest Request = (HttpWebRequest)WebRequest.Create("https://api.telegram.org/bot" + Token + "/getUpdates?offset=-1");
 Response = (HttpWebResponse)Request.GetResponse();
}



 }
July 9th 19 at 11:27
2 answers
July 9th 19 at 11:29
Solution
You must leave the scheme with offset=-1.

"getUpdates" returns with the message update_id. This number is Your offset. On the server side, it always increases sequentially.
Use this offset, increment it to +1 with each request, returning the new message. You can save the number of the offset to the disk to survive restarts of the bot.

https://core.telegram.org/bots/api
> In order to avoid getting duplicate updates, recalculate the offset after each server response.
Thank you very much. Theoretically, everything is clear. Not quite panamu what you need in the query to write. Couldn't you tell? - tomas49 commented on July 9th 19 at 11:32
: Make a request as well as do now.
The query will return update_id, which You need to catch (in void Listen), moving to the screen or to a file. Add to the number of +1.
This will be Your offset tear-off point, below which You go down more will not. Make this number a global variable in the entire bot.

Then, all subsequent GetUpdates make as now, but with the 'offset=global_offset'
If the request returned a new message, add to the global offset unit. If new messages are not, then the response will return no messages at this offset-y. - Carli commented on July 9th 19 at 11:35
Everything has turned out. Thank you!))) - tomas49 commented on July 9th 19 at 11:38
: *gives five* - Carli commented on July 9th 19 at 11:41
July 9th 19 at 11:31
Here is the class bot for Telegram if suddenly someone need:
class TelegramBot
{
 private string Token;
 private byte[] Buffer = new byte[8192];

 private HttpWebResponse Response;
 private Stream ResponseStream;

 private static Timer TimeListen;

 public TelegramUpdates Update.
 public int GlobalOffset;


 ResposeString StringBuilder= new StringBuilder();


 public TelegramBot(string InputToken)
{
 Token = InputToken;

 TimeListen = new Timer(Listen, null, 0, 500);

try
{
 GlobalOffset = Convert.ToInt32(File.ReadAllText(@"..\OffsetKey.txt"));

 GlobalOffset += 1;
}

catch
{

 MessageBox.Show("file read Error");
}
}

 public void SendMessage(string InputMessage)
{

 WebRequest Request = (HttpWebRequest)WebRequest.Create("https://api.telegram.org/bot" + Token + "/sendMessage?chat_id=228451619&text="+InputMessage+"");
 Response = (HttpWebResponse)Request.GetResponse();
Request.Abort();

}

 public string GetResponse()
{
ResposeString.Clear();
 ResponseStream = Response.GetResponseStream();

 string tempString = null;
 int count = 0;


do
{

 count = ResponseStream.Read(Buffer, 0, Buffer.Length);
 if (count != 0)
{
 tempString = Encoding.ASCII.GetString(Buffer, 0, count);
ResposeString.Append(tempString);
}
 } while (count > 0);


 return ResposeString.ToString();
}

 public void GetUpdates(int InputOffset)
{
 WebRequest Request = (HttpWebRequest)WebRequest.Create("https://api.telegram.org/bot" + Token + "/getUpdates?offset="+InputOffset+"");
 Response = (HttpWebResponse)Request.GetResponse();
}

 private void Listen(object state)
{
GetUpdates(GlobalOffset);
try
{
 Update = JsonConvert.DeserializeObject<telegramupdates>(GetResponse());
 if (Update.result[0].message.text != "")
{


 File.WriteAllText(@"..\OffsetKey.txt", GlobalOffset.ToString());

 GlobalOffset += 1;


 #region BotLogic

 if (Update.result[0].message.text == "h") SendMessage("Hello");

#endregion
}

}

catch
{



}



}

 }</telegramupdates>

P. S: suggestions for improvement in comments.

Find more questions by tags Telegram