class Tourmaline::Client

Overview

The Client class is the base class for all Tourmaline based bots. Extend this class to create your own bots, or create an instance of Client and add commands and listenters to it.

Included Modules

Defined in:

tourmaline/client.cr
tourmaline/client/core.cr
tourmaline/client/games.cr
tourmaline/client/payments.cr
tourmaline/client/polls.cr
tourmaline/client/stickers.cr
tourmaline/client/webhook.cr

Constant Summary

API_URL = "https://api.telegram.org/"
Log = ::Log.for(self)

Constructors

Instance Method Summary

Constructor Detail

def self.new(api_key : String, updates_timeout : Int32 | Nil = nil, allowed_updates : Array(String) | Nil = nil, persistence : Persistence = NilPersistence.new) #

Create a new instance of Tourmaline::Client. It is highly recommended to set @api_key at an environment variable.


[View source]

Instance Method Detail

def add_event_handler(handler : EventHandler) #

[View source]
def add_sticker_to_set(user_id, name, emojis, png_sticker = nil, tgs_sticker = nil, mask_position = nil) #

Use this method to add a new sticker to a set created by the bot. Returns true on success.


[View source]
def answer_callback_query(callback_query_id, text = nil, show_alert = nil, url = nil, cache_time = nil) #

Use this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, true is returned.

Alternatively, the user can be redirected to the specified Game URL (url). For this option to work, you must first create a game for your bot via @Botfather and accept the terms. Otherwise, you may use links like t.me/your_bot?start=XXXX that open your bot with a parameter.


[View source]
def answer_inline_query(inline_query_id, results, cache_time = nil, is_personal = nil, next_offset = nil, switch_pm_text = nil, switch_pm_parameter = nil) #

Use this method to send answers to an inline query. On success, True is returned. No more than 50 results per query are allowed.


[View source]
def answer_pre_checkout_query(pre_checkout_query_id, ok, error_message = nil) #

Once the user has confirmed their payment and shipping details, the Client API sends the final confirmation in the form of a Update with the field pre_checkout_query. Use this method to respond to such pre-checkout queries. On success, true is returned.

Note: The Client API must receive an answer within 10 seconds after the pre-checkout query was sent.


[View source]
def answer_shipping_query(shipping_query_id, ok, shipping_options = nil, error_message = nil) #

If you sent an invoice requesting a shipping address and the parameter is_flexible was specified, the Client API will send a Update with a shipping_query field to the bot. Use this method to reply to shipping queries. On success, true is returned.


[View source]
def bot_name : String #

Gets the name of the Client at the time the Client was started. Refreshing can be done by setting @bot_name to get_me.username.to_s.


[View source]
def create_new_sticker_set(user_id, name, title, emojis, png_sticker = nil, tgs_sticker = nil, contains_masks = nil, mask_position = nil) #

Use this method to create new sticker set owned by a user. The bot will be able to edit the created sticker set. You must use exactly one of the fields png_sticker or tgs_sticker. Returns true on success.


[View source]
def delete_chat_photo(chat) #

Use this method to delete a chat photo. Photos can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns true on success.

Note: In regular groups (non-supergroups), this method will only work if the All Members Are Admins setting is off in the target group.


[View source]
def delete_chat_sticker_set(chat_id) #

Use this method to delete a group sticker set from a supergroup. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Use the field can_set_sticker_set optionally returned in #get_chat requests to check if the bot can use this method. Returns true on success.


[View source]
def delete_message(chat, message) #

Use this method to delete a Message, including service messages, with the following limitations:

  • A message can only be deleted if it was sent less than 48 hours ago.
  • Bots can delete outgoing messages in private chats, groups, and supergroups.
  • Bots can delete incoming messages in private chats.
  • Bots granted can_post_messages permissions can delete outgoing messages in channels.
  • If the bot is an administrator of a group, it can delete any message there.
  • If the bot has can_delete_messages permission in a supergroup or a channel, it can delete any message there. Returns true on success.

[View source]
def delete_sticker_from_set(sticker) #

Use this method to delete a sticker from a set created by the bot. Returns true on success.


[View source]
def delete_webhook #

Use this method to remove webhook integration if you decide to switch back to getUpdates.


[View source]
def edit_message_caption(chat, caption, message = nil, inline_message = nil, parse_mode = ParseMode::Normal, reply_markup = nil) #

Use this method to edit captions of messages. On success, if edited message is sent by the bot, the edited Message is returned, otherwise true is returned.


[View source]
def edit_message_live_location(chat, latitude, longitude, message = nil, inline_message = nil, reply_markup = nil) #

Use this method to edit live location messages. A location can be edited until its live_period expires or editing is explicitly disabled by a call to #stopMessageLiveLocation. On success, if the edited message wasn't by the bot, the edited Message is returned, otherwise true is returned.


[View source]
def edit_message_media(chat, media, message = nil, inline_message = nil, reply_markup = nil) #

[View source]
def edit_message_reply_markup(chat, message = nil, inline_message = nil, reply_markup = nil) #

Use this method to edit only the reply markup of messages. On success, if edited message is sent by the bot, the edited Message is returned, otherwise true is returned.


[View source]
def edit_message_text(chat, text, message = nil, inline_message = nil, parse_mode = ParseMode::Normal, disable_link_preview = false, reply_markup = nil) #

Use this method to edit text and game messages. On success, if edited message is sent by the bot, the edited Message is returned, otherwise true is returned.


[View source]
def endpoint_url : String #

[View source]
def endpoint_url=(endpoint_url : String) #

[View source]
def event_handlers : Array(EventHandler) #

[View source]
def export_chat_invite_link(chat) #

Use this method to generate a new invite link for a chat; any previously generated link is revoked. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns the new invite link as String on success.


[View source]
def forward_message(chat, from_chat, message, disable_notification = false) #

Use this method to forward messages of any kind. On success, the sent Message is returned.


[View source]
def get_chat(chat) #

Use this method to get up to date information about the chat (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.). Returns a Chat object on success.


[View source]
def get_chat_administrators(chat) #

Use this method to get a list of administrators in a chat. On success, returns an Array of ChatMember objects that contains information about all chat administrators except other bots. If the chat is a group or a supergroup and no administrators were appointed, only the creator will be returned.


[View source]
def get_chat_member(chat, user) #

Use this method to get information about a member of a chat. Returns a ChatMember object on success.


[View source]
def get_chat_members_count(chat) #

Use this method to get the number of members in a chat. Returns Int32 on success.


[View source]
def get_file(file_id) #

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. On success, a TFile object is returned. 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 #get_file again.

To simplify retrieving a link for a file, use the #get_file_link method.


[View source]
def get_file_link(file) #

Returns a download link for a TFile.


[View source]
def get_game_high_scores(user_id, chat_id = nil, message_id = nil, inline_message_id = nil) #

Use this method to get data for high score tables. Will return the score of the specified user and several of his neighbors in a game. On success, returns an Array of GameHighScore objects.

This method will currently return scores for the target user, plus two of his closest neighbors on each side. Will also return the top three users if the user and his neighbors are not among them. Please note that this behavior is subject to change.


[View source]
def get_me #

A simple method for testing your bot's auth token. Requires no parameters. Returns basic information about the bot in form of a User object.


[View source]
def get_my_commands #

Use this method to get the current list of the bot's commands. Requires no parameters. Returns Array of BotCommand on success.


[View source]
def get_sticker_set(name : String) #

Use this method to get a sticker set. On success, a StickerSet object is returned.


[View source]
def get_updates(offset = @next_offset, limit = nil, timeout = @updates_timeout, allowed_updates = @allowed_updates) #

Use this method to receive incoming updates using long polling (wiki). An Array of Update objects is returned.


[View source]
def get_user_profile_photos(user, offset = nil, limit = nil) #

Use this method to get a list of profile pictures for a user. Returns a UserProfilePhotos object.


[View source]
def get_webhook_info #

Use this method to get current webhook status. Requires no parameters. On success, returns a WebhookInfo object. If the bot is using #getUpdates, will return an object with the url field empty.


[View source]
def handle_update(update : Update) #

[View source]
def is_admin?(chat_id) #

Convenience method to check if this bot is an admin in the current chat. See Client#get_chat_administrators for more info.


[View source]
def kick_chat_member(chat, user, until_date = nil) #

Use this method to kick a user from a group, a supergroup or a channel. In the case of supergroups and channels, the user will not be able to return to the group on their own using invite links, etc., unless unbanned first. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns true on success.

Note: In regular groups (non-supergroups), this method will only work if the All Members Are Admins setting is off in the target group. Otherwise members may only be removed by the group's creator or by the member that added them.


[View source]
def labeled_prices(lp : Array(NamedTuple(label: String, amount: Int32))) #

Convenience method to create and Array of LabledPrice from an Array of NamedTuple(label: String, amount: Int32).


[View source]
def leave_chat(chat) #

Use this method for your bot to leave a group, supergroup, or channel. Returns true on success.


[View source]
def next_offset : Int64 #

[View source]
def persistence : Persistence #

[View source]
def pin_chat_message(chat, message, disable_notification = false) #

Use this method to pin a message in a group, a supergroup, or a channel. The bot must be an administrator in the chat for this to work and must have the can_pin_messages admin right in the supergroup or can_edit_messages admin right in the channel. Returns true on success.


[View source]
def poll #

Start polling for updates. This method uses a combination of #get_updates and #handle_update to send continuously check Telegram's servers for updates.


[View source]
def polling : Bool #

[View source]
def promote_chat_member(chat, user, until_date = nil, can_change_info = nil, can_post_messages = nil, can_edit_messages = nil, can_delete_messages = nil, can_invite_users = nil, can_restrict_members = nil, can_pin_messages = nil, can_promote_members = nil) #

Use this method to promote or demote a user in a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Pass False for all boolean parameters to demote a user. Returns true on success.


[View source]
def restrict_chat_member(chat, user, permissions, until_date = nil) #

Use this method to unban a previously kicked user in a supergroup or channel. The user will not return to the group or channel automatically, but will be able to join via link, etc. The bot must be an administrator for this to work. Returns true on success.


[View source]
def send_animation(chat, animation, duration = nil, width = nil, height = nil, thumb = nil, caption = nil, parse_mode = ParseMode::Normal, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

[View source]
def send_audio(chat, audio, caption = nil, duration = nil, preformer = nil, title = nil, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

Use this method to send audio files, if you want Telegram clients to display them in the music player. Your audio must be in the .mp3 format. On success, the sent Message is returned. Bots can currently send audio files of up to 50 MB in size, this limit may be changed in the future.

For sending voice messages, use the #sendVoice method instead.

TODO Add filesize checking and validation.


[View source]
def send_chat_action(chat, action : ChatAction) #

Use this method when you need to tell the user that something is happening on the bot's side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status). Returns true on success.

Example: The ImageBot needs some time to process a request and upload the image. Instead of sending a text message along the lines of “Retrieving image, please wait…”, the bot may use #sendChatAction with action = upload_photo. The user will see a “sending photo” status for the bot.

We only recommend using this method when a response from the bot will take a noticeable amount of time to arrive.


[View source]
def send_contact(chat, phone_number, first_name, last_name = nil, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

Use this method to send phone contacts. On success, the sent Message is returned.


[View source]
def send_dice(chat, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

Use this method to send a dice, which will have a random value from 1 to 6. On success, the sent Message is returned.


[View source]
def send_document(chat, document, caption = nil, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

Use this method to send general files. On success, the sent Message is returned. Bots can currently send files of any type of up to 50 MB in size, this limit may be changed in the future.

TODO Add filesize checking and validation.


[View source]
def send_game(chat_id, game_short_name, disable_notification = nil, reply_to_message_id = nil, reply_markup = nil) #

Use this method to send a game. On success, the sent Message is returned.


[View source]
def send_invoice(chat, title, description, payload, provider_token, start_parameter, currency, prices, provider_data = nil, photo_url = nil, photo_size = nil, photo_width = nil, photo_height = nil, need_name = nil, need_shipping_address = nil, send_phone_number_to_provider = nil, send_email_to_provider = nil, is_flexible = nil, disable_notification = nil, reply_to_message = nil, reply_markup = nil) #

Use this method to send invoices. On success, the sent Message is returned.


[View source]
def send_location(chat, latitude, longitude, live_period = nil, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

Use this method to send point on the map. On success, the sent Message is returned.


[View source]
def send_media_group(chat, media : Array(InputMediaPhoto | InputMediaVideo), disable_notification = false, reply_to_message = nil) #

Use this method to send a group of photos or videos as an album. On success, an array of the sent Messages is returned.


[View source]
def send_message(chat, text, parse_mode = ParseMode::Normal, link_preview = false, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

Use this method to send text messages. On success, the sent Message is returned.


[View source]
def send_photo(chat, photo, caption = nil, parse_mode = ParseMode::Normal, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

Use this method to send photos. On success, the sent Message is returned.


[View source]
def send_poll(chat, question : String, options : Array(String), anonymous : Bool = true, type : PollType = PollType::Regular, allows_multiple_answers : Bool = false, correct_option_id : Int32 | Nil = nil, closed : Bool = false, disable_notification : Bool = false, reply_to_message = nil, reply_markup = nil) #

Use this method to send a native poll. On success, the sent Message is returned.


[View source]
def send_sticker(chat_id : Int32 | String, sticker : InputFile | String, disable_notification : Bool | Nil = nil, reply_to_message_id : Int32 | Nil = nil, reply_markup = nil) #

Use this method to send .webp stickers. On success, the sent Message is returned.

See: https://core.telegram.org/bots/api#stickers for more info.


[View source]
def send_venue(chat, latitude, longitude, title, address, foursquare_id = nil, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

Use this method to send information about a venue. On success, the sent Message is returned.


[View source]
def send_video(chat, video, duration = nil, width = nil, height = nil, caption = nil, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). On success, the sent Message is returned. Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.

TODO Add filesize checking and validation.


[View source]
def send_video_note(chat, video_note, duration = nil, width = nil, height = nil, caption = nil, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

As of v.4.0, Telegram clients support rounded square mp4 videos of up to 1 minute long. Use this method to send video messages. On success, the sent Message is returned.


[View source]
def send_voice(chat, voice, caption = nil, duration = nil, preformer = nil, title = nil, disable_notification = false, reply_to_message = nil, reply_markup = nil) #

Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.

TODO Add filesize checking and validation.


[View source]
def serve(address = "127.0.0.1", port = 8080, ssl_certificate_path = nil, ssl_key_path = nil) #

Start an HTTP server at the specified address and port that listens for updates using Telegram's webhooks. This is the reccommended way to handle bots in production.


[View source]
def set_chat_admininstrator_custom_title(chat, user, custom_title) #

Use this method to set a custom title for an administrator in a supergroup promoted by the bot. Returns True on success.


[View source]
def set_chat_description(chat, description) #

Use this method to change the description of a supergroup or a channel. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns true on success.


[View source]
def set_chat_permissions(chat, permissions) #

Use this method to set default chat permissions for all members. The bot must be an administrator in the group or a supergroup for this to work and must have the can_restrict_members admin rights. Returns True on success.


[View source]
def set_chat_photo(chat, photo) #

Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns true on success.

Note: In regular groups (non-supergroups), this method will only work if the All Members Are Admins setting is off in the target group.


[View source]
def set_chat_sticker_set(chat_id, sticker_set_name) #

Use this method to set a new group sticker set for a supergroup. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Use the field can_set_sticker_set optionally returned in #get_chat requests to check if the bot can use this method. Returns true on success.


[View source]
def set_chat_title(chat, title) #

Use this method to change the title of a chat. Titles can't be changed for private chats. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. Returns true on success.

Note: In regular groups (non-supergroups), this method will only work if the All Members Are Admins setting is off in the target group.


[View source]
def set_game_score(user_id, score, force = false, disable_edit_message = nil, chat_id = nil, message_id = nil, inline_message_id = nil) #

Use this method to set the score of the specified user in a game. On success, if the message was sent by the bot, returns the edited Message, otherwise returns true.

Raises an error, if the new score is not greater than the user's current score in the chat and force is false (default).


[View source]
def set_my_commands(commands : Array(BotCommand | NamedTuple(command: String, description: String))) #

Use this method to change the list of the bot's commands. Returns true on success.


[View source]
def set_sticker_position_in_set(sticker, position) #

Use this method to move a sticker in a set created by the bot to a specific position. Returns true on success.


[View source]
def set_sticker_set_thumb(name, user, thumb = nil) #

Use this method to set the thumbnail of a sticker set. Animated thumbnails can be set for animated sticker sets only. Returns true on success.


[View source]
def set_webhook(url, certificate = nil, max_connections = nil, allowed_updates = @allowed_updates) #

Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. Returns true on success.

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. https://www.example.com/<token>. Since nobody else knows your bot‘s token, you can be pretty sure it’s us.


[View source]
def shipping_options(options : Array(NamedTuple(id: String, title: String, prices: Array(LabeledPrice)))) #

Convenience method to create an Array of ShippingOption from a NamedTuple(id: String, title: String, prices: Array(LabeledPrice)).


[View source]
def stop_message_live_location(chat, message = nil, inline_message = nil, reply_markup = nil) #

Use this method to stop updating a live location message before live_period expires. On success, if the message was sent by the bot, the sent Message is returned, otherwise true is returned.


[View source]
def stop_poll(chat, message, reply_markup = nil) #

Use this method to stop a poll which was sent by the bot. On success, the stopped Poll with the final results is returned.


[View source]
def stop_polling #

Stops the bot from polling.


[View source]
def unban_chat_member(chat, user) #

Use this method to unban a previously kicked user in a supergroup or channel. The user will not return to the group or channel automatically, but will be able to join via link, etc. The bot must be an administrator for this to work. Returns true on success.


[View source]
def unpin_chat_message(chat) #

Use this method to unpin a message in a group, a supergroup, or a channel. The bot must be an administrator in the chat for this to work and must have the ‘can_pin_messages’ admin right in the supergroup or ‘can_edit_messages’ admin right in the channel. Returns true on success.


[View source]
def unset_webhook #

Use this to unset the webhook and stop receiving updates to your bot.


[View source]
def upload_sticker_file(user_id, png_sticker) #

Use this method to upload a .png file with a sticker for later use in #create_new_sticker_set and #add_sticker_to_set methods (can be used multiple times). Returns the uploaded TFile on success.


[View source]