Class: GrubY::API
- Inherits:
-
Object
- Object
- GrubY::API
- Defined in:
- lib/gruubY/api.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- METHOD_ALIASES =
{ "set_bot_commands" => "setMyCommands", "get_bot_commands" => "getMyCommands", "delete_bot_commands" => "deleteMyCommands", "set_bot_name" => "setMyName", "get_bot_name" => "getMyName", "set_bot_info_description" => "setMyDescription", "get_bot_info_description" => "getMyDescription", "set_bot_info_short_description" => "setMyShortDescription", "get_bot_info_short_description" => "getMyShortDescription", "set_bot_default_privileges" => "setMyDefaultAdministratorRights", "get_bot_default_privileges" => "getMyDefaultAdministratorRights", "get_chat_members_count" => "getChatMemberCount", "get_chat_photos" => "getUserProfilePhotos", "get_chat_audios" => "getUserProfileAudios", "set_emoji_status" => "setUserEmojiStatus", "get_managed_bot_token" => "getManagedBotToken", "replace_managed_bot_token" => "replaceManagedBotToken", "ban_chat_member" => "banChatMember", "unban_chat_member" => "unbanChatMember", "restrict_chat_member" => "restrictChatMember", "promote_chat_member" => "promoteChatMember", "set_administrator_title" => "setChatAdministratorCustomTitle", "set_chat_member_tag" => "setChatMemberTag", "set_chat_menu_button" => "setChatMenuButton", "get_chat_menu_button" => "getChatMenuButton", "send_reaction" => "setMessageReaction", "set_message_reaction" => "setMessageReaction", "export_chat_invite_link" => "exportChatInviteLink", "create_chat_invite_link" => "createChatInviteLink", "edit_chat_invite_link" => "editChatInviteLink", "revoke_chat_invite_link" => "revokeChatInviteLink", "approve_chat_join_request" => "approveChatJoinRequest", "decline_chat_join_request" => "declineChatJoinRequest", "send_message_draft" => "sendMessageDraft", "send_paid_reaction" => "sendPaidReaction", "get_user_chat_boosts" => "getUserChatBoosts", "get_business_connection" => "getBusinessConnection", "forward_media_group" => "forwardMessages", "copy_media_group" => "copyMessages" }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #answer_callback(callback_query_id, **opts) ⇒ Object
- #answer_callback_query(callback_query_id, **opts) ⇒ Object
- #copy_media_group(chat_id, from_chat_id, message_ids, **opts) ⇒ Object
- #copy_message(chat_id, from_chat_id, message_id, **opts) ⇒ Object
- #copy_messages(chat_id, from_chat_id, message_ids, **opts) ⇒ Object
- #delete_message(chat_id, message_id) ⇒ Object
- #delete_messages(chat_id, message_ids, **opts) ⇒ Object
- #download_file(file_path, save_as) ⇒ Object
- #edit_message_text(chat_id, message_id, text, **opts) ⇒ Object
- #forward_media_group(chat_id, from_chat_id, message_ids, **opts) ⇒ Object
- #forward_message(chat_id, from_chat_id, message_id, **opts) ⇒ Object
- #forward_messages(chat_id, from_chat_id, message_ids, **opts) ⇒ Object
- #get_file(file_id) ⇒ Object
- #get_updates(offset = nil, timeout: 30, **opts) ⇒ Object
- #get_web_app_link_url(**opts) ⇒ Object
- #get_web_app_url(**opts) ⇒ Object
-
#initialize(token) ⇒ API
constructor
A new instance of API.
- #method_missing(name, *args, **kwargs, &block) ⇒ Object
- #open_web_app(**opts) ⇒ Object
- #raw(method, params = {}) ⇒ Object
- #request(method, params = {}) ⇒ Object
- #respond_to_missing?(_name, _include_private = false) ⇒ Boolean
- #send_animation(chat_id, animation, **opts) ⇒ Object
- #send_audio(chat_id, audio, **opts) ⇒ Object
- #send_cached_media(chat_id, file_id, **opts) ⇒ Object
- #send_chat_action(chat_id, action, **opts) ⇒ Object
- #send_checklist(chat_id, checklist, business_connection_id:, **opts) ⇒ Object
- #send_contact(chat_id, phone_number, first_name, **opts) ⇒ Object
- #send_dice(chat_id, **opts) ⇒ Object
- #send_document(chat_id, document, **opts) ⇒ Object
- #send_location(chat_id, latitude, longitude, **opts) ⇒ Object
- #send_media_group(chat_id, media, **opts) ⇒ Object
- #send_message(chat_id, text, **opts) ⇒ Object
- #send_message_draft(chat_id, draft_id, text, **opts) ⇒ Object
- #send_paid_media(chat_id, star_count, media, **opts) ⇒ Object
- #send_photo(chat_id, photo, **opts) ⇒ Object
- #send_poll(chat_id, question, options, **opts) ⇒ Object
- #send_reaction(chat_id, message_id, **opts) ⇒ Object
- #send_screenshot_notification(chat_id, **opts) ⇒ Object
- #send_sticker(chat_id, sticker, **opts) ⇒ Object
- #send_venue(chat_id, latitude, longitude, title, address, **opts) ⇒ Object
- #send_video(chat_id, video, **opts) ⇒ Object
- #send_video_note(chat_id, video_note, **opts) ⇒ Object
- #send_voice(chat_id, voice, **opts) ⇒ Object
- #set_message_reaction(chat_id, message_id, **opts) ⇒ Object
Constructor Details
#initialize(token) ⇒ API
Returns a new instance of API.
51 52 53 54 |
# File 'lib/gruubY/api.rb', line 51 def initialize(token) @token = token @base = URI("https://api.telegram.org/bot#{token}/") end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **kwargs, &block) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/gruubY/api.rb', line 294 def method_missing(name, *args, **kwargs, &block) return super if block params = {} if args.length == 1 && args.first.is_a?(Hash) params.merge!(args.first) elsif !args.empty? return super end params.merge!(kwargs) unless kwargs.empty? request(self.class.camelize_api_method(name), params) end |
Class Method Details
.camelize_api_method(name) ⇒ Object
324 325 326 327 328 329 330 331 332 |
# File 'lib/gruubY/api.rb', line 324 def self.camelize_api_method(name) alias_name = METHOD_ALIASES[name.to_s] return alias_name if alias_name parts = name.to_s.split("_") return name.to_s if parts.empty? parts.first + parts[1..].map(&:capitalize).join end |
.escape_html(text) ⇒ Object
316 317 318 319 320 321 322 |
# File 'lib/gruubY/api.rb', line 316 def self.escape_html(text) text.to_s .gsub("&", "&") .gsub("<", "<") .gsub(">", ">") .gsub('"', """) end |
.escape_markdown_v2(text) ⇒ Object
312 313 314 |
# File 'lib/gruubY/api.rb', line 312 def self.escape_markdown_v2(text) text.to_s.gsub(/([_\*\[\]\(\)~`>#+\-=|{}.!\\])/, '\\\\\1') end |
Instance Method Details
#answer_callback(callback_query_id, **opts) ⇒ Object
268 269 270 |
# File 'lib/gruubY/api.rb', line 268 def answer_callback(callback_query_id, **opts) answer_callback_query(callback_query_id, **opts) end |
#answer_callback_query(callback_query_id, **opts) ⇒ Object
262 263 264 265 266 |
# File 'lib/gruubY/api.rb', line 262 def answer_callback_query(callback_query_id, **opts) request("answerCallbackQuery", { callback_query_id: callback_query_id }.merge(opts)) end |
#copy_media_group(chat_id, from_chat_id, message_ids, **opts) ⇒ Object
125 126 127 |
# File 'lib/gruubY/api.rb', line 125 def copy_media_group(chat_id, from_chat_id, , **opts) (chat_id, from_chat_id, , **opts) end |
#copy_message(chat_id, from_chat_id, message_id, **opts) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/gruubY/api.rb', line 109 def (chat_id, from_chat_id, , **opts) request("copyMessage", { chat_id: chat_id, from_chat_id: from_chat_id, message_id: }.merge(opts)) end |
#copy_messages(chat_id, from_chat_id, message_ids, **opts) ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/gruubY/api.rb', line 117 def (chat_id, from_chat_id, , **opts) request("copyMessages", { chat_id: chat_id, from_chat_id: from_chat_id, message_ids: }.merge(opts)) end |
#delete_message(chat_id, message_id) ⇒ Object
280 281 282 283 284 285 |
# File 'lib/gruubY/api.rb', line 280 def (chat_id, ) request("deleteMessage", { chat_id: chat_id, message_id: }) end |
#delete_messages(chat_id, message_ids, **opts) ⇒ Object
287 288 289 290 291 292 |
# File 'lib/gruubY/api.rb', line 287 def (chat_id, , **opts) request("deleteMessages", { chat_id: chat_id, message_ids: }.merge(opts)) end |
#download_file(file_path, save_as) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/gruubY/api.rb', line 77 def download_file(file_path, save_as) url = URI("https://api.telegram.org/file/bot#{@token}/#{file_path}") File.open(save_as, "wb") do |file| file.write(Net::HTTP.get(url)) end save_as end |
#edit_message_text(chat_id, message_id, text, **opts) ⇒ Object
272 273 274 275 276 277 278 |
# File 'lib/gruubY/api.rb', line 272 def (chat_id, , text, **opts) request("editMessageText", { chat_id: chat_id, message_id: , text: text }.merge(opts)) end |
#forward_media_group(chat_id, from_chat_id, message_ids, **opts) ⇒ Object
105 106 107 |
# File 'lib/gruubY/api.rb', line 105 def forward_media_group(chat_id, from_chat_id, , **opts) (chat_id, from_chat_id, , **opts) end |
#forward_message(chat_id, from_chat_id, message_id, **opts) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/gruubY/api.rb', line 89 def (chat_id, from_chat_id, , **opts) request("forwardMessage", { chat_id: chat_id, from_chat_id: from_chat_id, message_id: }.merge(opts)) end |
#forward_messages(chat_id, from_chat_id, message_ids, **opts) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/gruubY/api.rb', line 97 def (chat_id, from_chat_id, , **opts) request("forwardMessages", { chat_id: chat_id, from_chat_id: from_chat_id, message_ids: }.merge(opts)) end |
#get_file(file_id) ⇒ Object
73 74 75 |
# File 'lib/gruubY/api.rb', line 73 def get_file(file_id) request("getFile", { file_id: file_id }) end |
#get_updates(offset = nil, timeout: 30, **opts) ⇒ Object
67 68 69 70 71 |
# File 'lib/gruubY/api.rb', line 67 def get_updates(offset = nil, timeout: 30, **opts) params = { timeout: timeout }.merge(opts) params[:offset] = offset if offset request("getUpdates", params) end |
#get_web_app_link_url(**opts) ⇒ Object
223 224 225 |
# File 'lib/gruubY/api.rb', line 223 def get_web_app_link_url(**opts) request("getWebAppLinkUrl", opts) end |
#get_web_app_url(**opts) ⇒ Object
227 228 229 |
# File 'lib/gruubY/api.rb', line 227 def get_web_app_url(**opts) request("getWebAppUrl", opts) end |
#open_web_app(**opts) ⇒ Object
231 232 233 |
# File 'lib/gruubY/api.rb', line 231 def open_web_app(**opts) request("openWebApp", opts) end |
#raw(method, params = {}) ⇒ Object
61 62 63 64 65 |
# File 'lib/gruubY/api.rb', line 61 def raw(method, params = {}) uri = @base + method.to_s response = Retry.call { Net::HTTP.post_form(uri, serialize_params(params)) } parse_response(response) end |
#request(method, params = {}) ⇒ Object
56 57 58 59 |
# File 'lib/gruubY/api.rb', line 56 def request(method, params = {}) payload = raw(method, params) payload["result"] end |
#respond_to_missing?(_name, _include_private = false) ⇒ Boolean
308 309 310 |
# File 'lib/gruubY/api.rb', line 308 def respond_to_missing?(_name, _include_private = false) true end |
#send_animation(chat_id, animation, **opts) ⇒ Object
145 146 147 |
# File 'lib/gruubY/api.rb', line 145 def send_animation(chat_id, animation, **opts) request("sendAnimation", { chat_id: chat_id, animation: animation }.merge(opts)) end |
#send_audio(chat_id, audio, **opts) ⇒ Object
133 134 135 |
# File 'lib/gruubY/api.rb', line 133 def send_audio(chat_id, audio, **opts) request("sendAudio", { chat_id: chat_id, audio: audio }.merge(opts)) end |
#send_cached_media(chat_id, file_id, **opts) ⇒ Object
161 162 163 |
# File 'lib/gruubY/api.rb', line 161 def send_cached_media(chat_id, file_id, **opts) request("sendDocument", { chat_id: chat_id, document: file_id }.merge(opts)) end |
#send_chat_action(chat_id, action, **opts) ⇒ Object
247 248 249 |
# File 'lib/gruubY/api.rb', line 247 def send_chat_action(chat_id, action, **opts) request("sendChatAction", { chat_id: chat_id, action: action }.merge(opts)) end |
#send_checklist(chat_id, checklist, business_connection_id:, **opts) ⇒ Object
215 216 217 218 219 220 221 |
# File 'lib/gruubY/api.rb', line 215 def send_checklist(chat_id, checklist, business_connection_id:, **opts) request("sendChecklist", { business_connection_id: business_connection_id, chat_id: chat_id, checklist: checklist }.merge(opts)) end |
#send_contact(chat_id, phone_number, first_name, **opts) ⇒ Object
199 200 201 202 203 204 205 |
# File 'lib/gruubY/api.rb', line 199 def send_contact(chat_id, phone_number, first_name, **opts) request("sendContact", { chat_id: chat_id, phone_number: phone_number, first_name: first_name }.merge(opts)) end |
#send_dice(chat_id, **opts) ⇒ Object
235 236 237 |
# File 'lib/gruubY/api.rb', line 235 def send_dice(chat_id, **opts) request("sendDice", { chat_id: chat_id }.merge(opts)) end |
#send_document(chat_id, document, **opts) ⇒ Object
137 138 139 |
# File 'lib/gruubY/api.rb', line 137 def send_document(chat_id, document, **opts) request("sendDocument", { chat_id: chat_id, document: document }.merge(opts)) end |
#send_location(chat_id, latitude, longitude, **opts) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/gruubY/api.rb', line 181 def send_location(chat_id, latitude, longitude, **opts) request("sendLocation", { chat_id: chat_id, latitude: latitude, longitude: longitude }.merge(opts)) end |
#send_media_group(chat_id, media, **opts) ⇒ Object
177 178 179 |
# File 'lib/gruubY/api.rb', line 177 def send_media_group(chat_id, media, **opts) request("sendMediaGroup", { chat_id: chat_id, media: media }.merge(opts)) end |
#send_message(chat_id, text, **opts) ⇒ Object
85 86 87 |
# File 'lib/gruubY/api.rb', line 85 def (chat_id, text, **opts) request("sendMessage", { chat_id: chat_id, text: text }.merge(opts)) end |
#send_message_draft(chat_id, draft_id, text, **opts) ⇒ Object
239 240 241 242 243 244 245 |
# File 'lib/gruubY/api.rb', line 239 def (chat_id, draft_id, text, **opts) request("sendMessageDraft", { chat_id: chat_id, draft_id: draft_id, text: text }.merge(opts)) end |
#send_paid_media(chat_id, star_count, media, **opts) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/gruubY/api.rb', line 169 def send_paid_media(chat_id, star_count, media, **opts) request("sendPaidMedia", { chat_id: chat_id, star_count: star_count, media: media }.merge(opts)) end |
#send_photo(chat_id, photo, **opts) ⇒ Object
129 130 131 |
# File 'lib/gruubY/api.rb', line 129 def send_photo(chat_id, photo, **opts) request("sendPhoto", { chat_id: chat_id, photo: photo }.merge(opts)) end |
#send_poll(chat_id, question, options, **opts) ⇒ Object
207 208 209 210 211 212 213 |
# File 'lib/gruubY/api.rb', line 207 def send_poll(chat_id, question, , **opts) request("sendPoll", { chat_id: chat_id, question: question, options: }.merge(opts)) end |
#send_reaction(chat_id, message_id, **opts) ⇒ Object
258 259 260 |
# File 'lib/gruubY/api.rb', line 258 def send_reaction(chat_id, , **opts) (chat_id, , **opts) end |
#send_screenshot_notification(chat_id, **opts) ⇒ Object
165 166 167 |
# File 'lib/gruubY/api.rb', line 165 def send_screenshot_notification(chat_id, **opts) request("sendScreenshotNotification", { chat_id: chat_id }.merge(opts)) end |
#send_sticker(chat_id, sticker, **opts) ⇒ Object
157 158 159 |
# File 'lib/gruubY/api.rb', line 157 def send_sticker(chat_id, sticker, **opts) request("sendSticker", { chat_id: chat_id, sticker: sticker }.merge(opts)) end |
#send_venue(chat_id, latitude, longitude, title, address, **opts) ⇒ Object
189 190 191 192 193 194 195 196 197 |
# File 'lib/gruubY/api.rb', line 189 def send_venue(chat_id, latitude, longitude, title, address, **opts) request("sendVenue", { chat_id: chat_id, latitude: latitude, longitude: longitude, title: title, address: address }.merge(opts)) end |
#send_video(chat_id, video, **opts) ⇒ Object
141 142 143 |
# File 'lib/gruubY/api.rb', line 141 def send_video(chat_id, video, **opts) request("sendVideo", { chat_id: chat_id, video: video }.merge(opts)) end |
#send_video_note(chat_id, video_note, **opts) ⇒ Object
153 154 155 |
# File 'lib/gruubY/api.rb', line 153 def send_video_note(chat_id, video_note, **opts) request("sendVideoNote", { chat_id: chat_id, video_note: video_note }.merge(opts)) end |
#send_voice(chat_id, voice, **opts) ⇒ Object
149 150 151 |
# File 'lib/gruubY/api.rb', line 149 def send_voice(chat_id, voice, **opts) request("sendVoice", { chat_id: chat_id, voice: voice }.merge(opts)) end |
#set_message_reaction(chat_id, message_id, **opts) ⇒ Object
251 252 253 254 255 256 |
# File 'lib/gruubY/api.rb', line 251 def (chat_id, , **opts) request("setMessageReaction", { chat_id: chat_id, message_id: }.merge(opts)) end |