setWebhook method
Future<bool>
setWebhook(
- String url, {
- String? ip_address,
- File? certificate,
- int? max_connections,
- List<
String> ? allowed_updates, - bool? drop_pending_updates,
inherited
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 request comes from Telegram,
we recommend using a secret path in the URL, e.g. https://www.example.com/<token>
.
Since nobody else knows your bot‘s token, you can be pretty sure it’s us.
Notes
- You will not be able to receive updates using getUpdates for as long as an outgoing webhook is set up.
- 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.
- Ports currently supported for Webhooks: 443, 80, 88, 8443.
NEW! If you're having any trouble setting up webhooks, please check out this amazing guide to Webhooks.
Implementation
Future<bool> setWebhook(String url,
{String? ip_address,
io.File? certificate,
int? max_connections,
List<String>? allowed_updates,
bool? drop_pending_updates}) async {
var requestUrl = _apiUri('setWebhook');
var body = <String, dynamic>{
'url': url,
'ip_address': ip_address,
'max_connections': max_connections,
'allowed_updates':
allowed_updates == null ? null : jsonEncode(allowed_updates),
'drop_pending_updates': drop_pending_updates,
};
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);
}
}