예제 #1
0
        /// <summary>
        /// Sends a message to the given target (user or group chat)
        /// </summary>
        /// <param name="target">A User or GroupChat</param>
        /// <param name="messageText">The text of the message</param>
        /// <param name="disableLinkPreview">Whether or not to disable link previews</param>
        /// <param name="replyTarget">The message to reply to</param>
        /// <param name="forceReplyOptions">Specifies that the message must be replied to, and what users must reply</param>
        /// <returns>The message that was sent.</returns>
        public Message SendMessage(MessageTarget target, string messageText, bool disableLinkPreview = false, Message replyTarget = null, ForceReplyOptions forceReplyOptions = null)
        {
            var request = new RestRequest("sendMessage", Method.POST)
            {
                RootElement = "result"
            };

            request.AddParameter("chat_id", target.Id);
            request.AddParameter("text", messageText);
            request.AddParameter("disable_web_page_preview", disableLinkPreview);

            if (replyTarget != null)
            {
                request.AddParameter("reply_to_message_id", replyTarget.MessageId);
            }

            if (forceReplyOptions != null)
            {
                request.AddParameter("reply_markup", SimpleJson.SerializeObject(forceReplyOptions));
            }

            var result = _client.Execute<Message>(request);
            return result.Data;
        }
예제 #2
0
        /// <summary>
        /// Forward a message from one chat to another.
        /// </summary>
        /// <param name="message">The message to forward.</param>
        /// <param name="target">The user/group to send to.</param>
        /// <returns>The message that was forwarded</returns>
        public Message ForwardMessage(Message message, MessageTarget target)
        {
            var request = new RestRequest("forwardMessage", Method.POST)
            {
                RootElement = "result"
            };

            request.AddParameter("chat_id", target.Id);
            request.AddParameter("from_chat_id", (message.Chat == null ? message.From.Id : message.Chat.Id));
            request.AddParameter("message_id", message.MessageId);

            var result = _client.Execute<Message>(request);
            return result.Data;
        }
예제 #3
0
        /// <summary>
        /// Sends a photo to the given target (user or group chat)
        /// </summary>
        /// <param name="target">A User or GroupChat</param>
        /// <param name="imageStream">A stream containing the data for the image</param>
        /// <param name="caption">A caption for the image</param>
        /// <param name="filename">The filename to report to Telegram</param>
        /// <returns>The message that was sent.</returns>
        public Message SendPhoto(MessageTarget target, Stream imageStream, string caption = null, string filename = "picture.jpg")
        {
            if (filename == null) { throw new ArgumentNullException("filename", "Lectern requires a filename with proper extension."); }
            if (!filename.Contains(".")) { throw new ArgumentException("Lectern requires a filename with proper extension.", "filename"); }

            var request = new RestRequest("sendPhoto", Method.POST)
            {
                RootElement = "result"
            };

            request.AddParameter("chat_id", target.Id);
            request.AddFile("photo", imageStream.CopyTo, filename);
            request.AddParameter("caption", caption);

            request.AddHeader("Content-Type", "multipart/form-data");

            var result = _client.Execute<Message>(request);

            imageStream.Close();
            return result.Data;
        }
예제 #4
0
        /// <summary>
        /// Indicates that the bot is doing a specified action
        /// </summary>
        /// <param name="target">The target to indicate towards</param>
        /// <param name="action">The action the bot is doing (from the ChatAction class)</param>
        public void SendChatAction(MessageTarget target, string action)
        {
            var request = new RestRequest("sendChatAction", Method.POST);

            request.AddParameter("chat_id", target.Id);
            request.AddParameter("action", action);

            _client.Execute(request);
        }