public async Task <HttpResponseMessage> PostAsync(TelegramMessage message, string chatId, ParseMode parseMode)
        {
            var payload = new
            {
                chat_id    = chatId,
                text       = message.Text,
                parse_mode = parseMode.ToString()
            };
            var json     = JsonSerializer.Serialize(value: payload);
            var response = await _httpClient.PostAsync(requestUri : _apiUrl,
                                                       content : new StringContent(content: json, encoding: Encoding.UTF8, mediaType: "application/json"));

            return(response);
        }
Exemple #2
0
        /// <summary>Sends a text message.</summary>
        /// <param name="chatId">
        /// Unique identifier for the message recipient or username of the target channel (in the format
        /// @channelusername).
        /// </param>
        /// <param name="text">Text of the message to be sent.</param>
        /// <param name="parseMode">
        /// Indicates the way that the Telegram should parse the sent message.
        /// </param>
        /// <param name="disableWebPagePreview">
        /// if set to <c>true</c> disables link previews for links in this message.
        /// </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> SendMessageAsync([NotNull] string chatId, [NotNull] string text, ParseMode parseMode = ParseMode.Normal, bool disableWebPagePreview = false, bool disableNotification = false, long replyToMessageId = 0, IReply replyMarkup = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Contracts.EnsureNotNull(chatId, nameof(chatId));
            Contracts.EnsureNotNull(text, nameof(text));

            var parameters = new NameValueCollection {
                { "text", text }
            };

            parameters.AddIf(disableWebPagePreview, "disable_web_page_preview", true);
            parameters.AddIf(parseMode != ParseMode.Normal, "parse_mode", parseMode.ToString());

            return(this.CallTelegramMethodAsync <Message>(cancellationToken, "sendMessage", parameters, chatId, replyToMessageId, replyMarkup, disableNotification));
        }
Exemple #3
0
 public Task <HttpResponseMessage> EditMessageAsync(
     long messageId,
     string text,
     ParseMode parseMode,
     bool disableWebPagePreview,
     IReplyMarkup replyMarkup)
 {
     return(MakeRequestAsync("editMessageText",
                             new EditMessageRequest(ChatId, messageId, text)
     {
         ParseMode = parseMode.ToString(),
         DisableWebPagePreview = disableWebPagePreview,
         ReplyMarkup = replyMarkup
     }));
 }
        /// <summary>
        /// Edits text messages sent by the bot or via the bot (for inline bots).
        /// </summary>
        /// <param name="chatId">Unique identifier for the target chat or username of the target channel (in the format @channelusername). Required if <paramref name="inlineMessageId"/> is not specified.</param>
        /// <param name="messageId">Unique identifier of the sent message. Required if <paramref name="inlineMessageId"/> is not specified.</param>
        /// <param name="inlineMessageId">The identifier of the inline message. Required if <paramref name="chatId"/> and <paramref name="messageId"/> are not specified.</param>
        /// <param name="text">New text of the message</param>
        /// <param name="parseMode">
        /// A value from <see cref="ParseMode"/> enum indicates the way that the Telegram should parse the sent message.
        /// Send <see cref="ParseMode.Markdown"/>, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message.
        /// </param>
        /// <param name="disableWebPagePreview">Disables link previews for links in this message</param>
        /// <param name="replyMarkup">An <see cref="InlineKeyboardMarkup" /> object for a custom reply keyboard.</param>
        /// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation. The task results contains the edited <see cref="Message" /> on success.
        /// </returns>
        private Task <Message> EditMessageTextAsync(string chatId, long messageId, string inlineMessageId, [NotNull] string text, ParseMode parseMode = ParseMode.Normal, bool disableWebPagePreview = false, InlineKeyboardMarkup replyMarkup = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Contracts.EnsureNotNull(text, nameof(text));

            var parameters = new NameValueCollection();

            parameters.AddIf(!string.IsNullOrWhiteSpace(chatId), "chat_id", chatId);
            parameters.AddIf(messageId > 0, "message_id", messageId.ToString());
            parameters.AddIf(!string.IsNullOrWhiteSpace(inlineMessageId), "inline_message_id", inlineMessageId);
            parameters.Add("text", text);
            parameters.AddIf(parseMode != ParseMode.Normal, "parse_mode", parseMode.ToString());
            parameters.AddIf(disableWebPagePreview, "disable_web_page_preview", true);

            return(this.CallTelegramMethodAsync <Message>(cancellationToken, "editMessageText", parameters, replyMarkup: replyMarkup));
        }
Exemple #5
0
 public Task <HttpResponseMessage> SendMessageAsync(
     string text,
     ParseMode parseMode,
     bool disableWebPagePreview,
     bool disableNotification,
     long replyToMessageId,
     IReplyMarkup replyMarkup)
 {
     return(MakeRequestAsync("sendMessage",
                             new SendMessageRequest(ChatId, text)
     {
         ParseMode = parseMode.ToString(),
         DisableWebPagePreview = disableWebPagePreview,
         DisableNotification = disableNotification,
         ReplyToMessageId = replyToMessageId,
         ReplyMarkup = replyMarkup
     }));
 }
Exemple #6
0
        public IObservable <ChatPostMessageResponse> PostMessage(string channelId, string text,
                                                                 string username = null, bool?asUser      = null, ParseMode parse  = ParseMode.Default,
                                                                 bool?linkNames  = null, bool?unfurlLinks = null, bool?unfurlMedia = null,
                                                                 Uri iconUrl     = null, string iconEmoji = null, IEnumerable <Attachment> attachments = null)
        {
            channelId.ThrowIfNull("channelId");
            text.ThrowIfNull("text");

            var queryParams = new Dictionary <string, object>
            {
                { "channel", channelId },
                { "text", text },
                { "username", username },
                { "as_user", asUser.HasValue ? asUser.ToString().ToLower() : null },
                { "parse", parse == ParseMode.Default ? null : parse.ToString().ToLower() },
                { "unfurl_links", unfurlLinks.HasValue ? unfurlLinks.ToString().ToLower() : null },
                { "unfurl_media", unfurlMedia.HasValue ? unfurlMedia.ToString().ToLower() : null },
                { "icon_url", iconUrl != null?iconUrl.ToString() : null },
                { "icon_emoji", iconEmoji },
            };

            if (linkNames.HasValue)
            {
                queryParams.Add("link_names", Convert.ToInt32(linkNames.Value));
            }

            if (attachments != null)
            {
                var settings = new JsonSerializerSettings
                {
                    DefaultValueHandling = DefaultValueHandling.Ignore,
                    ContractResolver     = new CamelCasePropertyNamesContractResolver(),
                };

                queryParams.Add("attachments", JsonConvert.SerializeObject(attachments, settings));
            }

            return(ObservableApiCall(POST_MESSAGE_METHOD, queryParams,
                                     async(requestUrl, cancellationToken) => await GetResponseAsync <ChatPostMessageResponse>(requestUrl, cancellationToken)));
        }
        /// <summary>Sends a text message.</summary>
        /// <param name="chatId">
        /// Unique identifier for the message recipient or username of the target channel (in the format
        /// @channelusername).
        /// </param>
        /// <param name="text">Text of the message to be sent.</param>
        /// <param name="parseMode">
        /// Indicates the way that the Telegram should parse the sent message.
        /// </param>
        /// <param name="disableWebPagePreview">
        /// if set to <c>true</c> disables link previews for links in this message.
        /// </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> SendMessageAsync([NotNull] string chatId, [NotNull] string text, ParseMode parseMode = ParseMode.Normal, bool disableWebPagePreview = false, bool disableNotification = false, long replyToMessageId = 0, IReply replyMarkup = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Contracts.EnsureNotNull(chatId, nameof(chatId));
            Contracts.EnsureNotNull(text, nameof(text));

            var parameters = new NameValueCollection { { "text", text } };
            parameters.AddIf(disableWebPagePreview, "disable_web_page_preview", true);
            parameters.AddIf(parseMode != ParseMode.Normal, "parse_mode", parseMode.ToString());

            return this.CallTelegramMethodAsync<Message>(cancellationToken, "sendMessage", parameters, chatId, replyToMessageId, replyMarkup, disableNotification);
        }