From 9ebe30cb9121d8a6108ba085045fa74cd211e1d7 Mon Sep 17 00:00:00 2001 From: nextel Date: Fri, 13 Sep 2019 13:59:15 +0300 Subject: [PATCH 1/3] add fields to struct --- src/core/requests/send_poll.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/core/requests/send_poll.rs b/src/core/requests/send_poll.rs index 8341bb39..7090ef9b 100644 --- a/src/core/requests/send_poll.rs +++ b/src/core/requests/send_poll.rs @@ -1,8 +1,16 @@ -use crate::core::requests::RequestContext; -//TODO:: need implementation +use crate::core::requests::{RequestContext, ChatId}; + +///Use this method to send a native poll. A native poll can't be sent to a private chat. On success, the sent Message is returned. #[derive(Debug, Clone, Serialize)] struct SendPoll<'a> { #[serde(skip_serializing)] ctx: RequestContext<'a>, + + chat_id :ChatId, Yes Unique identifier for the target chat or username of the target channel (in the format @channelusername). A native poll can't be sent to a private chat. + question :String String Yes Poll question, 1-255 characters +options Vec Array of String Yes List of answer options, 2-10 strings 1-100 characters each +disable_notification Boolean Optional Sends the message silently. Users will receive a notification with no sound. +reply_to_message_id Integer Optional If the message is a reply, ID of the original message +reply_markup InlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardRemove or ForceReply Optional Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user. } From b2fb7c29636d5690e3d047f485e45e46a5fca930 Mon Sep 17 00:00:00 2001 From: nextel Date: Fri, 13 Sep 2019 15:42:45 +0300 Subject: [PATCH 2/3] add SendPoll implementation --- src/core/requests/send_poll.rs | 118 ++++++++++++++++++++++++++++++--- 1 file changed, 108 insertions(+), 10 deletions(-) diff --git a/src/core/requests/send_poll.rs b/src/core/requests/send_poll.rs index 7090ef9b..9a7f13e2 100644 --- a/src/core/requests/send_poll.rs +++ b/src/core/requests/send_poll.rs @@ -1,16 +1,114 @@ -use crate::core::requests::{RequestContext, ChatId}; +use crate::core::network; +use crate::core::requests::{ + ChatId, Request, RequestContext, RequestFuture, ResponseResult, +}; +use crate::core::types::{Message, ReplyMarkup}; - -///Use this method to send a native poll. A native poll can't be sent to a private chat. On success, the sent Message is returned. +/// Use this method to send a native poll. A native poll can't be sent to a +/// private chat. On success, the sent Message is returned. #[derive(Debug, Clone, Serialize)] struct SendPoll<'a> { #[serde(skip_serializing)] ctx: RequestContext<'a>, - - chat_id :ChatId, Yes Unique identifier for the target chat or username of the target channel (in the format @channelusername). A native poll can't be sent to a private chat. - question :String String Yes Poll question, 1-255 characters -options Vec Array of String Yes List of answer options, 2-10 strings 1-100 characters each -disable_notification Boolean Optional Sends the message silently. Users will receive a notification with no sound. -reply_to_message_id Integer Optional If the message is a reply, ID of the original message -reply_markup InlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardRemove or ForceReply Optional Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user. + /// identifier for the target chat or username of the target channel (in + /// the format @channelusername). A native poll can't be sent to a private + /// chat. + chat_id: ChatId, + /// Poll question, 1-255 characters + question: String, + /// List of answer options, 2-10 strings 1-100 characters each + options: Vec, + /// Sends the message silently. Users will receive a notification with no + /// sound. + disable_notification: Option, + /// If the message is a reply, ID of the original message + reply_to_message_id: Option, + /// InlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardRemove + /// or ForceReply Optional Additional interface options. A JSON-serialized + /// object for an inline keyboard, custom reply keyboard, instructions to + /// remove reply keyboard or to force a reply from the user. + reply_markup: Option, +} + +impl<'a> Request<'a> for SendPoll<'a> { + type ReturnValue = Message; + + fn send(self) -> RequestFuture<'a, ResponseResult> { + Box::pin(async move { + network::request_json( + &self.ctx.client, + &self.ctx.token, + "sendPoll", + &self, + ) + .await + }) + } +} + +impl<'a> SendPoll<'a> { + pub(crate) fn new( + ctx: RequestContext<'a>, + chat_id: ChatId, + question: String, + options: Vec, + ) -> Self { + Self { + ctx, + chat_id, + question, + options, + disable_notification: None, + reply_to_message_id: None, + reply_markup: None, + } + } + + pub fn chat_id(mut self, chat_id: T) -> Self + where + T: Into, + { + self.chat_id = chat_id.into(); + self + } + + pub fn question(mut self, question: T) -> Self + where + T: Into, + { + self.question = question.into(); + self + } + + pub fn options(mut self, options: T) -> Self + where + T: Into>, + { + self.options = options.into(); + self + } + + pub fn disable_notification(mut self, disable_notification: T) -> Self + where + T: Into>, + { + self.disable_notification = disable_notification.into(); + self + } + + pub fn reply_to_message_id(mut self, reply_to_message_id: T) -> Self + where + T: Into>, + { + self.reply_to_message_id = reply_to_message_id.into(); + self + } + + pub fn reply_markup(mut self, reply_markup: T) -> Self + where + T: Into, + { + self.reply_markup = Some(reply_markup.into()); + self + } } From d8c51bdde88f6a46e7069cea718f481d6dbd89ea Mon Sep 17 00:00:00 2001 From: nextel Date: Fri, 13 Sep 2019 15:45:19 +0300 Subject: [PATCH 3/3] fix option fields --- src/core/requests/send_poll.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/requests/send_poll.rs b/src/core/requests/send_poll.rs index 9a7f13e2..ba11325b 100644 --- a/src/core/requests/send_poll.rs +++ b/src/core/requests/send_poll.rs @@ -92,7 +92,7 @@ impl<'a> SendPoll<'a> { where T: Into>, { - self.disable_notification = disable_notification.into(); + self.disable_notification = Some(disable_notification.into()); self } @@ -100,7 +100,7 @@ impl<'a> SendPoll<'a> { where T: Into>, { - self.reply_to_message_id = reply_to_message_id.into(); + self.reply_to_message_id = Some(reply_to_message_id.into()); self }