setWebhook method

Future<bool> setWebhook(
  1. String url, {
  2. String? ipAddress,
  3. File? certificate,
  4. int? maxConnections,
  5. List<String>? allowedUpdates,
  6. bool? dropPendingUpdates,
  7. String? secretToken,
})

Use this method to specify a url and receive incoming updates via an outgoing webhook

Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. Returns True on success. If you'd like to make sure that the webhook was set by you, you can specify secret data in the parameter secretToken. If specified, the request will contain a header “X-Telegram-Bot-Api-Secret-Token” with the secret token as content.

Notes

  1. You will not be able to receive updates using getUpdates for as long as an outgoing webhook is set up.
  2. To use a self-signed certificate, you need to upload your public key certificate using certificate parameter. Please upload as InputFile, sending a String will not work.
  3. Ports currently supported for Webhooks: 443, 80, 88, 8443.

If you're having any trouble setting up webhooks, please check out this amazing guide to Webhooks.

https://core.telegram.org/bots/api#setwebhook

Implementation

Future<bool> setWebhook(String url,
    {String? ipAddress,
    io.File? certificate,
    int? maxConnections,
    List<String>? allowedUpdates,
    bool? dropPendingUpdates,
    String? secretToken}) async {
  var requestUrl = _apiUri('setWebhook');

  var body = <String, dynamic>{
    'url': url,
    'ip_address': ipAddress,
    'max_connections': maxConnections,
    'allowed_updates':
        allowedUpdates == null ? null : jsonEncode(allowedUpdates),
    'drop_pending_updates': dropPendingUpdates,
    'secret_token': secretToken,
  };
  if (certificate != null) {
    // filename cannot be empty to post to Telegram server
    var files = <MultipartFile>[];
    files.add(MultipartFile(
        'certificate', certificate.openRead(), certificate.lengthSync(),
        filename: '${certificate.lengthSync()}'));
    return await HttpClient.httpMultipartPost(requestUrl, files, body: body);
  } else {
    return await HttpClient.httpPost(requestUrl, body: body);
  }
}