コード例 #1
0
        /// <summary>
        /// Use this method to forward messages of any kind.
        /// </summary>
        /// <param name="chat_id">Unique identifier for the target chat or username of the target channel (in the format @channelusername)</param>
        /// <param name="from_chat_id">Unique identifier for the chat where the original message was sent (or channel username in the format @channelusername)</param>
        /// <param name="message_id">Message identifier in the chat specified in from_chat_id</param>
        /// <param name="disable_notification">Sends the message silently (https://telegram.org/blog/channels-2-0#silent-messages). Users will receive a notification with no sound.</param>
        /// <returns>On success, the sent Message is returned.</returns>
        public async Task <MessageClass> forwardMessage(string chat_id, string from_chat_id, long message_id, bool disable_notification = false)
        {
            forwardMessageJSON forward_msg_json = new forwardMessageJSON()
            {
                chat_id      = chat_id,
                from_chat_id = from_chat_id,
                message_id   = message_id
            };
            List <string> skip_fields = new List <string>();

            if (!disable_notification)
            {
                skip_fields.Add("disable_notification");
            }

            await SendRequest(nameof(forwardMessage), forward_msg_json.GetFiealds(skip_fields.ToArray()));

            if (string.IsNullOrEmpty(http_response_raw))
            {
                return(null);
            }
            forwardMessageJSON.Result result = (forwardMessageJSON.Result) await SerialiserJSON.ReadObject(typeof(forwardMessageJSON.Result), http_response_raw);

            return(result.result);
        }
コード例 #2
0
        /// <summary>
        /// A simple method for testing your bot's auth token. Requires no parameters.
        /// </summary>
        /// <returns>Returns basic information about the bot in form of a User (https://core.telegram.org/bots/api#user) object.</returns>
        public async Task <UserClass> getMe()
        {
            await SendRequest(nameof(getMe), null);

            if (string.IsNullOrEmpty(http_response_raw))
            {
                return(null);
            }
            getMeJSON.Result result = (getMeJSON.Result) await SerialiserJSON.ReadObject(typeof(getMeJSON.Result), http_response_raw);

            return(result.result);
        }
コード例 #3
0
        /// <summary>
        /// Use this method to send text messages.
        /// </summary>
        /// <param name="chat_id">Unique identifier for the target chat or username of the target channel (in the format @channelusername)</param>
        /// <param name="text">Optional	Send Markdown or HTML, if you want Telegram apps to show bold, italic, fixed-width text or inline URLs in your bot's message.</param>
        /// <param name="parse_mode">Send Markdown (https://core.telegram.org/bots/api#markdown-style) or HTML (https://core.telegram.org/bots/api#html-style), if you want Telegram apps to show bold, italic, fixed-width text or inline URLs (https://core.telegram.org/bots/api#formatting-options) in your bot's message.
        /// Formatting options
        /// The Bot API supports basic formatting for messages. You can use bold and italic text, as well as inline links and pre-formatted code in your bots' messages. Telegram clients will render them accordingly. You can use either markdown-style or HTML-style formatting.
        ///
        /// Note that Telegram clients will display an alert to the user before opening an inline link (‘Open this link?’ together with the full URL).
        ///
        /// Links 'tg://user?id=<user_id>' can be used to mention a user by their id without using a username. Please note:
        ///
        /// These links will work only if they are used inside an inline link.
        /// These mentions are only guaranteed to work if the user has contacted the bot in the past or is a member in the group where he was mentioned.
        /// Markdown style
        /// To use this mode, pass Markdown in the parse_mode field when using sendMessage. Use the following syntax in your message:
        ///
        /// *bold text*
        /// _italic text_
        /// [inline URL](http://www.example.com/)
        /// [inline mention of a user](tg://user?id=123456789)
        /// `inline fixed-width code`
        /// ```block_language
        /// pre-formatted fixed-width code block
        /// ```
        /// HTML style
        /// To use this mode, pass HTML in the parse_mode field when using sendMessage. The following tags are currently supported:
        ///
        /// <b>bold</b>, <strong>bold</strong>
        /// <i>italic</i>, <em>italic</em>
        /// <a href="http://www.example.com/">inline URL</a>
        /// <a href="tg://user?id=123456789">inline mention of a user</a>
        /// <code>inline fixed-width code</code>
        /// <pre>pre-formatted fixed-width code block</pre>
        /// Please note:
        ///
        /// Only the tags mentioned above are currently supported.
        /// Tags must not be nested.
        /// All <, > and & symbols that are not a part of a tag or an HTML entity must be replaced with the corresponding HTML entities (< with &lt;, > with &gt; and & with &amp;).
        /// All numerical HTML entities are supported.
        /// The API currently supports only the following named HTML entities: &lt;, &gt;, &amp; and &quot;.
        /// </param>
        /// <param name="disable_web_page_preview">Optional	Disables link previews for links in this message</param>
        /// <param name="disable_notification">Optional	Sends the message silently (https://telegram.org/blog/channels-2-0#silent-messages). Users will receive a notification with no sound.</param>
        /// <param name="reply_to_message_id">Optional	If the message is a reply, ID of the original message</param>
        /// <param name="reply_markup">InlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardRemove or ForceReply [Optional] Additional interface options. A JSON-serialized object for an inline keyboard (https://core.telegram.org/bots#inline-keyboards-and-on-the-fly-updating), custom reply keyboard (https://core.telegram.org/bots#keyboards), instructions to remove reply keyboard or to force a reply from the user.</param>
        /// <returns>On success, the sent Message (https://core.telegram.org/bots/api#message) is returned.</returns>
        public async Task <MessageClass> sendMessage(string chat_id, string text, ParseModes?parse_mode = null, bool disable_web_page_preview = false, bool disable_notification = false, long reply_to_message_id = 0, object reply_markup = null)
        {
            sendMessageJSON send_msg_json = new sendMessageJSON()
            {
                chat_id = chat_id,
                text    = text
            };

            List <string> skip_fields = new List <string>();

            if (parse_mode is null)
            {
                skip_fields.Add("parse_mode");
            }
            else
            {
                send_msg_json.parse_mode = parse_mode.ToString();
            }

            if (!disable_notification)
            {
                skip_fields.Add("disable_notification");
            }

            if (reply_to_message_id == 0)
            {
                skip_fields.Add("reply_to_message_id");
            }
            else
            {
                send_msg_json.reply_to_message_id = reply_to_message_id;
            }

            if (reply_markup == null)
            {
                skip_fields.Add("reply_markup");
            }

            await SendRequest(nameof(sendMessage), send_msg_json.GetFiealds(skip_fields.ToArray()));

            if (string.IsNullOrEmpty(http_response_raw))
            {
                return(null);
            }
            sendMessageJSON.Result result = (sendMessageJSON.Result) await SerialiserJSON.ReadObject(typeof(sendMessageJSON.Result), http_response_raw);

            return(result.result);
        }
コード例 #4
0
        /// <summary>
        /// Use this method to get basic info about a file and prepare it for downloading. For the moment, bots can download files of up to 20MB in size. The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>, where <file_path> is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile again.
        /// Note: This function may not preserve the original file name and MIME type. You should save the file's MIME type and name (if available) when the File object is received.
        /// </summary>
        /// <param name="file_id">File identifier to get info about</param>
        /// <returns>On success, a File object is returned.</returns>
        public async Task <FileClass> getFile(string file_id)
        {
            getFileJSON get_file_json = new getFileJSON()
            {
                file_id = file_id
            };

            await SendRequest(nameof(getFile), get_file_json.GetFiealds(new string[0]));

            if (string.IsNullOrEmpty(http_response_raw.Trim()))
            {
                return(null);
            }
            getFileJSON.Result result = (getFileJSON.Result) await SerialiserJSON.ReadObject(typeof(getFileJSON.Result), http_response_raw);

            return(result.result);
        }
コード例 #5
0
        /// <summary>
        /// Получить входящие обновления в бот
        /// </summary>
        /// <param name="_limit">Димит на получение данных</param>
        /// <returns></returns>
        public async Task <Update[]> getUpdates(int _limit = 10)
        {
            getUpdatesJSON updates_filter = new getUpdatesJSON()
            {
                offset          = offset + 1,
                limit           = _limit,
                allowed_updates = new string[0]
            };

            await Task.Run(async() =>
            {
                await SendRequest(nameof(getUpdates), updates_filter.GetFiealds(new string[0]));
            });

            if (string.IsNullOrEmpty(http_response_raw))
            {
                return(new Update[0]);
            }

            getUpdatesJSON.Result result = (getUpdatesJSON.Result) await SerialiserJSON.ReadObject(typeof(getUpdatesJSON.Result), http_response_raw);

            return(result.result);
        }
コード例 #6
0
        /// <summary>
        /// Use this method to get a list of administrators in a chat. If the chat is a group or a supergroup and no administrators were appointed, only the creator will be returned.
        /// </summary>
        /// <param name="chat_id">Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername)</param>
        /// <returns>On success, returns an Array of ChatMember objects that contains information about all chat administrators except other bots.</returns>
        public async Task <ChatMemberClass[]> getChatAdministrators(string chat_id)
        {
            getChatAdministratorsJSON get_chat_administrators_json = new getChatAdministratorsJSON()
            {
                chat_id = chat_id
            };

            await SendRequest(nameof(getChatAdministrators), get_chat_administrators_json.GetFiealds(new string[0]));

            if (string.IsNullOrEmpty(http_response_raw.Trim()))
            {
                return(null);
            }
            getChatAdministratorsJSON.Result result = (getChatAdministratorsJSON.Result) await SerialiserJSON.ReadObject(typeof(getChatAdministratorsJSON.Result), http_response_raw);

            return(result.result);
        }
コード例 #7
0
        /// <summary>
        /// Use this method to send photos
        /// </summary>
        /// <param name="chat_id">Unique identifier for the target chat or username of the target channel (in the format @channelusername)</param>
        /// <param name="photo">Photo to send. Pass a file_id as String to send a photo that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a photo from the Internet, or upload a new photo using multipart/form-data. More info on Sending Files » https://core.telegram.org/bots/api#sending-files </param>
        /// <param name="caption">Photo caption (may also be used when resending photos by file_id), 0-200 characters</param>
        /// <param name="disable_notification">Sends the message silently (https://telegram.org/blog/channels-2-0#silent-messages). Users will receive a notification with no sound.</param>
        /// <param name="reply_to_message_id">If the message is a reply, ID of the original message</param>
        /// <param name="reply_markup">InlineKeyboardMarkup or ReplyKeyboardMarkup or ReplyKeyboardRemove or ForceReply [Optional] Additional interface options. A JSON-serialized object for an inline keyboard (https://core.telegram.org/bots#inline-keyboards-and-on-the-fly-updating), custom reply keyboard (https://core.telegram.org/bots#keyboards), instructions to remove reply keyboard or to force a reply from the user.</param>
        /// <returns>On success, the sent Message is returned.</returns>
        public async Task <MessageClass> sendPhoto(string chat_id, object photo, string caption = null, ParseModes?parse_mode = null, bool disable_notification = false, long reply_to_message_id = -1, object reply_markup = null)
        {
            sendPhotoJSON send_photo_json = new sendPhotoJSON()
            {
                chat_id = chat_id
            };

            List <string> skip_fields = new List <string>();

            if (string.IsNullOrEmpty(caption))
            {
                skip_fields.Add("caption");
            }
            else
            {
                send_photo_json.caption = caption;
            }

            if (parse_mode is null)
            {
                skip_fields.Add("parse_mode");
            }
            else
            {
                send_photo_json.parse_mode = parse_mode.ToString();
            }

            if (!disable_notification)
            {
                skip_fields.Add("disable_notification");
            }

            if (reply_to_message_id == 0)
            {
                skip_fields.Add("reply_to_message_id");
            }
            else
            {
                send_photo_json.reply_to_message_id = reply_to_message_id.ToString();
            }

            if (reply_markup == null)
            {
                skip_fields.Add("reply_markup");
            }


            if (photo is string || photo is int)
            {
                send_photo_json.photo = photo;
                await SendRequest(nameof(sendPhoto), send_photo_json.GetFiealds(skip_fields.ToArray()));

                if (string.IsNullOrEmpty(http_response_raw))
                {
                    return(null);
                }
            }
            else if (photo is InputFileClass)
            {
                skip_fields.Add("photo");
                await SendRequest(nameof(sendPhoto), send_photo_json.GetFiealds(skip_fields.ToArray()), (InputFileClass)photo);

                if (string.IsNullOrEmpty(http_response_raw))
                {
                    return(null);
                }
            }
            sendPhotoJSON.Result result = (sendPhotoJSON.Result) await SerialiserJSON.ReadObject(typeof(sendPhotoJSON.Result), http_response_raw);

            return(result.result);
        }