@@ -126,35 +126,36 @@ class Webhook extends AbstractUpdateFetcher {
126126 allowed_updates: allowed_updates);
127127 }
128128
129- Future <void > setWebhook () async {
130- await telegram.setWebhook ('$url :$port $secretPath ' ,
131- ip_address: ip_address,
132- certificate: uploadCertificate ? certificate : null ,
133- max_connections: max_connections,
134- allowed_updates: allowed_updates,
135- drop_pending_updates: drop_pending_updates);
136- }
129+ Future <bool > setWebhook () async =>
130+ await telegram.setWebhook ('$url :$port $secretPath ' ,
131+ ip_address: ip_address,
132+ certificate: uploadCertificate ? certificate : null ,
133+ max_connections: max_connections,
134+ allowed_updates: allowed_updates,
135+ drop_pending_updates: drop_pending_updates);
137136
138137 /// Apply webhook configuration on Telegram API, and start the webhook server.
139138 @override
140139 Future <void > start () async {
141- await setWebhook ();
142-
143- _server.listen ((io.HttpRequest request) {
144- if (request.method == 'POST' && request.uri.path == secretPath) {
145- request.cast <List <int >>().transform (utf8.decoder).join ().then ((data) {
146- emitUpdate (Update .fromJson (jsonDecode (data)));
140+ if (await setWebhook ()) {
141+ _server.listen ((io.HttpRequest request) {
142+ if (request.method == 'POST' && request.uri.path == secretPath) {
143+ request.cast <List <int >>().transform (utf8.decoder).join ().then ((data) {
144+ emitUpdate (Update .fromJson (jsonDecode (data)));
145+ request.response
146+ ..write (jsonEncode ({'ok' : true }))
147+ ..close ();
148+ });
149+ } else {
147150 request.response
148- ..write (jsonEncode ({'ok' : true }))
151+ ..statusCode = io.HttpStatus .methodNotAllowed
152+ ..write (jsonEncode ({'ok' : false }))
149153 ..close ();
150- });
151- } else {
152- request.response
153- ..statusCode = io.HttpStatus .methodNotAllowed
154- ..write (jsonEncode ({'ok' : false }))
155- ..close ();
156- }
157- });
154+ }
155+ });
156+ } else {
157+ throw WebhookException ('Telegram API returns an error while attempting to set the webhook.' );
158+ }
158159 }
159160
160161 /// Remove webhook configuration from Telegram API, and stop the webhook server.
0 commit comments