-
Notifications
You must be signed in to change notification settings - Fork 5
/
TeleBot.cs
206 lines (176 loc) · 8.25 KB
/
TeleBot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using TeleBotDotNet.Json;
using TeleBotDotNet.Log;
using TeleBotDotNet.Requests.Methods;
using TeleBotDotNet.Requests.Methods.Bases;
using TeleBotDotNet.Responses.Methods;
using TeleBotDotNet.Responses.Types;
namespace TeleBotDotNet
{
/// <summary>
/// API implementation of January 20, 2016.
/// For documentation, please see https://core.telegram.org/bots/api.
/// </summary>
public class TeleBot
{
private LogEngine _log;
public TeleBot(string apiToken, bool enableLog = false)
{
ApiToken = apiToken;
Log.Enabled = enableLog;
}
public LogEngine Log => _log ?? (_log = new LogEngine());
internal static string ApiUrl => "https://api.telegram.org";
internal string ApiToken { get; }
private dynamic ExecuteAction(BaseMethodRequest request)
{
const string httpNewLine = "\r\n";
var webRequest = WebRequest.Create($"{ApiUrl}/bot{ApiToken}/{request.MethodName}");
// If the request is a GetUpdatesRequest, the timeout property can be set for long polling.
// Set the timeout property of the WebRequest to the same time, so the WebRequest won't
// timeout before the API does. This way other requests will still timeout on time.
var updatesRequest = request as GetUpdatesRequest;
if (updatesRequest?.Timeout != null)
{
const int milliSecondsInSecond = 1000;
webRequest.Timeout += updatesRequest.Timeout.Value * milliSecondsInSecond;
}
webRequest.Method = "POST";
var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
boundary = "--" + boundary;
using (var requestStream = webRequest.GetRequestStream())
{
var options = request.Parse();
// Write the values
foreach (var parameter in options.Parameters)
{
var buffer = Encoding.ASCII.GetBytes(boundary + httpNewLine);
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.ASCII.GetBytes($"Content-Disposition: form-data; name=\"{parameter.Key}\"{httpNewLine}{httpNewLine}");
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.UTF8.GetBytes(parameter.Value + httpNewLine);
requestStream.Write(buffer, 0, buffer.Length);
}
// Write the files
foreach (var file in options.Files)
{
var buffer = Encoding.ASCII.GetBytes(boundary + httpNewLine);
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.UTF8.GetBytes($"Content-Disposition: form-data; name=\"{file.Key}\"; filename=\"{file.FileName}\"{httpNewLine}");
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.ASCII.GetBytes($"Content-Type: {file.ContentType}{httpNewLine}{httpNewLine}");
requestStream.Write(buffer, 0, buffer.Length);
new MemoryStream(file.File).CopyTo(requestStream);
buffer = Encoding.ASCII.GetBytes(httpNewLine);
requestStream.Write(buffer, 0, buffer.Length);
}
var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
}
try
{
return DecodeWebResponse(webRequest.GetResponse());
}
catch (WebException e)
{
return DecodeWebResponse(e.Response);
}
}
private static JsonData DecodeWebResponse(WebResponse webResponse)
{
using (webResponse)
using (var responseStream = webResponse.GetResponseStream())
using (var stream = new MemoryStream())
{
responseStream?.CopyTo(stream);
var responseString = Encoding.UTF8.GetString(stream.ToArray());
return JsonData.Deserialize(responseString);
}
}
public GetMeResponse GetMe(GetMeRequest getMeRequest)
{
Log.Info(nameof(GetMe));
return GetMeResponse.Parse(ExecuteAction(getMeRequest));
}
public SendMessageResponse SendMessage(SendMessageRequest sendMessageRequest)
{
Log.Info(nameof(SendMessage));
return SendMessageResponse.Parse(ExecuteAction(sendMessageRequest));
}
public ForwardMessageResponse ForwardMessage(ForwardMessageRequest forwardMessageRequest)
{
Log.Info(nameof(ForwardMessage));
return ForwardMessageResponse.Parse(ExecuteAction(forwardMessageRequest));
}
public SendPhotoResponse SendPhoto(SendPhotoRequest sendPhotoRequest)
{
Log.Info(nameof(SendPhoto));
return SendPhotoResponse.Parse(ExecuteAction(sendPhotoRequest));
}
public SendAudioResponse SendAudio(SendAudioRequest sendAudioRequest)
{
Log.Info(nameof(SendAudio));
return SendAudioResponse.Parse(ExecuteAction(sendAudioRequest));
}
public SendDocumentResponse SendDocument(SendDocumentRequest sendDocumentRequest)
{
Log.Info(nameof(SendDocument));
return SendDocumentResponse.Parse(ExecuteAction(sendDocumentRequest));
}
public SendStickerResponse SendSticker(SendStickerRequest sendStickerRequest)
{
Log.Info(nameof(SendSticker));
return SendStickerResponse.Parse(ExecuteAction(sendStickerRequest));
}
public SendVideoResponse SendVideo(SendVideoRequest sendVideoRequest)
{
Log.Info(nameof(SendVideo));
return SendVideoResponse.Parse(ExecuteAction(sendVideoRequest));
}
public SendLocationResponse SendLocation(SendLocationRequest sendLocationRequest)
{
Log.Info(nameof(SendLocation));
return SendLocationResponse.Parse(ExecuteAction(sendLocationRequest));
}
public SendChatActionResponse SendChatAction(SendChatActionRequest sendChatActionRequest)
{
Log.Info(nameof(SendChatAction));
return SendChatActionResponse.Parse(ExecuteAction(sendChatActionRequest));
}
public GetUserProfilePhotosResponse GetUserProfilePhotos(GetUserProfilePhotosRequest getUserProfilePhotosRequest)
{
Log.Info(nameof(GetUserProfilePhotos));
return GetUserProfilePhotosResponse.Parse(ExecuteAction(getUserProfilePhotosRequest));
}
public GetUpdatesResponse GetUpdates(GetUpdatesRequest getUpdatesRequest)
{
Log.Info(nameof(GetUpdates));
return GetUpdatesResponse.Parse(ExecuteAction(getUpdatesRequest));
}
public SetWebhookResponse SetWebhook(SetWebhookRequest setWebhookRequest)
{
Log.Info(nameof(SetWebhook));
return SetWebhookResponse.Parse(ExecuteAction(setWebhookRequest));
}
public UpdateResponse ConvertWebhookResponse(string json)
{
Log.Info(nameof(ConvertWebhookResponse));
return UpdateResponse.Parse(JsonData.Deserialize(json));
}
public GetFileResponse GetFile(GetFileRequest getFileRequest)
{
Log.Info(nameof(GetFile));
return GetFileResponse.Parse(ExecuteAction(getFileRequest));
}
public AnswerInlineQueryResponse AnswerInlineQuery(AnswerInlineQueryRequest answerInlineQueryRequest)
{
Log.Info(nameof(AnswerInlineQuery));
return AnswerInlineQueryResponse.Parse(ExecuteAction(answerInlineQueryRequest));
}
}
}