Beispiel #1
0
        /// <summary>
        /// Use this method to specify a url and receive incoming updates via an outgoing webhook.
        /// </summary>
        /// <param name="url">
        /// HTTPS url to send updates to. Use an empty string to remove webhook integration
        /// </param>
        /// <param name="certificatePath">
        /// The fully qualified path to the certificate path so that the root certificate in use can be
        /// checked.
        /// </param>
        /// <param name="maxConnections">
        /// Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery,
        /// 1-100. Defaults to 40. Use lower values to limit the load on your bot‘s server, and higher
        /// values to increase your bot’s throughput.
        /// </param>
        /// <param name="cancellationToken">
        /// A cancellation token that can be used by other objects or threads to receive notice of
        /// cancellation.
        /// </param>
        /// <returns>A task that represents the asynchronous operation.</returns>
        /// <remarks>
        /// Whenever there is an update for the bot, Telegram will send an HTTPS POST request to the specified
        /// url, containing a JSON-serialized <see cref="Update" />. In case of an unsuccessful request, it
        /// will give up after a reasonable amount of attempts.
        /// <para>
        /// 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. <c>www.example.com/YOUR_TOKEN</c>. Since nobody else knows your bot's
        /// token, you can be pretty sure it’s Telegram.
        /// </para>
        /// <list type="bullet">
        ///     <item>
        ///         <description>
        /// You will not be able to receive updates using <see cref="GetUpdatesAsync" /> for as long as an
        /// outgoing webhook is set up.
        ///         </description>
        ///     </item><item>
        ///         <description>
        /// To use a self-signed certificate, you need to upload your public key certificate using
        /// <paramref name="certificatePath" /> parameter.
        ///         </description>
        ///     </item><item>
        ///         <description>
        /// Ports currently supported for Webhooks: 443, 80, 88, 8443.
        ///         </description>
        ///     </item>
        /// </list>
        /// </remarks>
        public async Task SetWebhookAsync(string url, string certificatePath = null, int maxConnections = 40, UpdateType[] allowedUpdates = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!string.IsNullOrWhiteSpace(certificatePath))
            {
                Contracts.EnsureFileExists(certificatePath);
            }

            using (var content = new MultipartFormDataContent())
            {
                if (!string.IsNullOrWhiteSpace(certificatePath))
                {
                    var fileName   = Path.GetFileName(certificatePath);
                    var fileStream = System.IO.File.Open(certificatePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    content.Add("certificate", fileStream, fileName);
                }

                if (maxConnections != 40)
                {
                    content.Add("max_connections", maxConnections);
                }
                content.Add("url", url);

                var response = await this.Client.PostAsync("setWebhook", content, cancellationToken).ConfigureAwait(false);

                EnsureSuccessStatusCode(response);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Sends an audio file to be displayed as a playable voice message on Telegram clients.
        /// </summary>
        /// <param name="chatId">Unique identifier for the message recipient or username of the target channel (in the format
        /// @channelusername).</param>
        /// <param name="filePath">Fully qualified path to the audio file.</param>
        /// <param name="caption"></param>
        /// <param name="duration">Duration of sent audio in seconds.</param>
        /// <param name="performer">The performer of the audio.</param>
        /// <param name="title">The track name.</param>
        /// <param name="disableNotification">If set to <c>true</c> sends the message silently. iOS users will not receive a notification, Android users will receive a notification with no sound.</param>
        /// <param name="replyToMessageId">If the message is a reply, ID of the original message.</param>
        /// <param name="replyMarkup">Additional interface options. An <see cref="IReply" /> object for a custom reply keyboard,
        /// instructions to hide keyboard or to force a reply from the user.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of
        /// cancellation.</param>
        /// <returns>
        /// On success, returns the sent <see cref="Message" />.
        /// </returns>
        public Task <Message> SendAudioFromFileAsync([NotNull] string chatId, [NotNull] string filePath, string caption = null, int duration = 0, string performer = null, string title = null, bool disableNotification = false, long replyToMessageId = 0, IReply replyMarkup = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Contracts.EnsureNotNull(chatId, nameof(chatId));
            Contracts.EnsureNotNull(filePath, nameof(filePath));
            Contracts.EnsureFileExists(filePath);

            var fileName   = Path.GetFileName(filePath);
            var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

            return(this.SendAudioAsync(chatId, fileStream, fileName, caption, duration, performer, title, disableNotification, replyToMessageId, replyMarkup, cancellationToken));
        }