Вот класс бота для Telegram, если вдруг кому-нибудь понадобиться:
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;
StringBuilder ResposeString= 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("Error file read");
}
}
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
{
}
}
}
P.S: предложения по улучшению оставляйте в комментариях.