@@ -117,12 +117,51 @@ String UniversalTelegramBot::sendGetToTelegram(const String& command) {
117
117
return mess;
118
118
}
119
119
120
+ bool UniversalTelegramBot::readHTTPAnswer (String &body, String &headers) {
121
+ int ch_count = 0 ;
122
+ long now = millis ();
123
+ bool finishedHeaders = false ;
124
+ bool currentLineIsBlank = true ;
125
+ bool responseReceived = false ;
126
+
127
+ while (millis () - now < waitForResponse) {
128
+ while (client->available ()) {
129
+ char c = client->read ();
130
+ responseReceived = true ;
131
+
132
+ if (!finishedHeaders) {
133
+ if (currentLineIsBlank && c == ' \n ' ) {
134
+ finishedHeaders = true ;
135
+ } else {
136
+ headers += c;
137
+ }
138
+ } else {
139
+ if (ch_count < maxMessageLength) {
140
+ body += c;
141
+ ch_count++;
142
+ }
143
+ }
144
+
145
+ if (c == ' \n ' ) currentLineIsBlank = true ;
146
+ else if (c != ' \r ' ) currentLineIsBlank = false ;
147
+ }
148
+
149
+ if (responseReceived) {
150
+ #ifdef _debug
151
+ Serial.println ();
152
+ Serial.println (body);
153
+ Serial.println ();
154
+ #endif
155
+ break ;
156
+ }
157
+ }
158
+ return responseReceived;
159
+ }
160
+
120
161
String UniversalTelegramBot::sendPostToTelegram (const String& command, JsonObject payload) {
121
162
122
163
String body;
123
164
String headers;
124
- long now;
125
- bool responseReceived = false ;
126
165
127
166
// Connect with api.telegram.org if not already connected
128
167
if (!client->connected ()) {
@@ -160,42 +199,7 @@ String UniversalTelegramBot::sendPostToTelegram(const String& command, JsonObjec
160
199
Serial.println (String (" Posting:" ) + out);
161
200
#endif
162
201
163
- int ch_count = 0 ;
164
- now = millis ();
165
- bool finishedHeaders = false ;
166
- bool currentLineIsBlank = true ;
167
- while (millis () - now < waitForResponse) {
168
- while (client->available ()) {
169
- char c = client->read ();
170
- responseReceived = true ;
171
-
172
- if (!finishedHeaders) {
173
- if (currentLineIsBlank && c == ' \n ' ) {
174
- finishedHeaders = true ;
175
- } else {
176
- headers += c;
177
- }
178
- } else {
179
- if (ch_count < maxMessageLength) {
180
- body += c;
181
- ch_count++;
182
- }
183
- }
184
-
185
- if (c == ' \n ' ) currentLineIsBlank = true ;
186
- else if (c != ' \r ' ) currentLineIsBlank = false ;
187
-
188
- }
189
-
190
- if (responseReceived) {
191
- #ifdef _debug
192
- Serial.println ();
193
- Serial.println (body);
194
- Serial.println ();
195
- #endif
196
- break ;
197
- }
198
- }
202
+ readHTTPAnswer (body, headers);
199
203
}
200
204
201
205
return body;
0 commit comments