Class: GrubY::Context
- Inherits:
-
Object
- Object
- GrubY::Context
- Defined in:
- lib/gruubY/context.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#update ⇒ Object
readonly
Returns the value of attribute update.
Instance Method Summary collapse
- #answer_callback(text = nil, show_alert: false) ⇒ Object
- #business_connection ⇒ Object
- #business_message ⇒ Object
- #callback_query ⇒ Object
- #chat_boost ⇒ Object
- #chat_id ⇒ Object
- #chat_join_request ⇒ Object
- #chat_member_updated ⇒ Object
- #chosen_inline_result ⇒ Object
- #command ⇒ Object
- #command_args ⇒ Object
- #deleted_business_messages ⇒ Object
- #deleted_messages ⇒ Object
- #download(file_name = "file.dat") ⇒ Object
- #edited_business_message ⇒ Object
- #edited_message ⇒ Object
- #error_payload ⇒ Object
- #file_id ⇒ Object
- #from ⇒ Object
-
#initialize(update, api, session = nil) ⇒ Context
constructor
A new instance of Context.
- #inline_query ⇒ Object
- #managed_bot_updated ⇒ Object
- #message ⇒ Object
- #message_reaction ⇒ Object
- #message_reaction_count ⇒ Object
- #poll(question, options) ⇒ Object
- #poll_update ⇒ Object
- #pre_checkout_query ⇒ Object
- #purchased_paid_media ⇒ Object
- #raw(method, params = {}) ⇒ Object
- #reply(text, keyboard = nil) ⇒ Object
- #session_data ⇒ Object
- #set_session(value) ⇒ Object
- #shipping_query ⇒ Object
- #story_update ⇒ Object
- #text ⇒ Object
- #user ⇒ Object
- #user_status ⇒ Object
Constructor Details
#initialize(update, api, session = nil) ⇒ Context
Returns a new instance of Context.
11 12 13 14 15 |
# File 'lib/gruubY/context.rb', line 11 def initialize(update, api, session = nil) @update = update @api = api @session = session end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
9 10 11 |
# File 'lib/gruubY/context.rb', line 9 def api @api end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
9 10 11 |
# File 'lib/gruubY/context.rb', line 9 def session @session end |
#update ⇒ Object (readonly)
Returns the value of attribute update.
9 10 11 |
# File 'lib/gruubY/context.rb', line 9 def update @update end |
Instance Method Details
#answer_callback(text = nil, show_alert: false) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/gruubY/context.rb', line 170 def answer_callback(text = nil, show_alert: false) id = callback_query&.id return unless id params = { callback_query_id: id, show_alert: show_alert } params[:text] = text if text @api.raw("answerCallbackQuery", params) end |
#business_connection ⇒ Object
89 90 91 92 |
# File 'lib/gruubY/context.rb', line 89 def business_connection data = @update["business_connection"] wrap_type("BusinessConnection", data) end |
#business_message ⇒ Object
27 28 29 30 |
# File 'lib/gruubY/context.rb', line 27 def data = @update["business_message"] Message.new(data, api: @api) if data end |
#callback_query ⇒ Object
64 65 66 67 |
# File 'lib/gruubY/context.rb', line 64 def callback_query data = update["callback_query"] CallbackQuery.new(data, api: @api) if data end |
#chat_boost ⇒ Object
104 105 106 107 |
# File 'lib/gruubY/context.rb', line 104 def chat_boost data = @update["chat_boost"] || @update["chat_boost_updated"] wrap_type("ChatBoostUpdated", data) end |
#chat_id ⇒ Object
51 52 53 |
# File 'lib/gruubY/context.rb', line 51 def chat_id &.dig("chat", "id") end |
#chat_join_request ⇒ Object
84 85 86 87 |
# File 'lib/gruubY/context.rb', line 84 def chat_join_request data = update["chat_join_request"] ChatJoinRequest.new(data, api: @api) if data end |
#chat_member_updated ⇒ Object
109 110 111 112 |
# File 'lib/gruubY/context.rb', line 109 def chat_member_updated data = @update["chat_member_updated"] || @update["chat_member"] wrap_type("ChatMemberUpdated", data) end |
#chosen_inline_result ⇒ Object
114 115 116 117 |
# File 'lib/gruubY/context.rb', line 114 def chosen_inline_result data = @update["chosen_inline_result"] wrap_type("ChosenInlineResult", data) end |
#command ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/gruubY/context.rb', line 183 def command t = text return nil unless t&.start_with?("/") token = t.split(/\s+/, 2).first token.sub(%r{^/}, "").split("@", 2).first end |
#command_args ⇒ Object
191 192 193 194 195 196 197 198 199 |
# File 'lib/gruubY/context.rb', line 191 def command_args t = text return [] unless t _, rest = t.split(/\s+/, 2) return [] unless rest rest.strip.split(/\s+/) end |
#deleted_business_messages ⇒ Object
42 43 44 45 |
# File 'lib/gruubY/context.rb', line 42 def list = @update["deleted_business_messages"] Array(list).map { |m| Message.new(m, api: @api) } end |
#deleted_messages ⇒ Object
37 38 39 40 |
# File 'lib/gruubY/context.rb', line 37 def list = @update["deleted_messages"] Array(list).map { |m| Message.new(m, api: @api) } end |
#download(file_name = "file.dat") ⇒ Object
216 217 218 219 220 |
# File 'lib/gruubY/context.rb', line 216 def download(file_name = "file.dat") f = api.get_file(file_id) api.download_file(f["file_path"], file_name) file_name end |
#edited_business_message ⇒ Object
32 33 34 35 |
# File 'lib/gruubY/context.rb', line 32 def data = @update["edited_business_message"] Message.new(data, api: @api) if data end |
#edited_message ⇒ Object
22 23 24 25 |
# File 'lib/gruubY/context.rb', line 22 def data = @update["edited_message"] Message.new(data, api: @api) if data end |
#error_payload ⇒ Object
144 145 146 |
# File 'lib/gruubY/context.rb', line 144 def error_payload @update["error"] end |
#file_id ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/gruubY/context.rb', line 201 def file_id data = return nil unless data if data["photo"] data["photo"].last["file_id"] elsif data["video"] data["video"]["file_id"] elsif data["audio"] data["audio"]["file_id"] elsif data["document"] data["document"]["file_id"] end end |
#from ⇒ Object
55 56 57 58 |
# File 'lib/gruubY/context.rb', line 55 def from data = user_data User.new(data, api: @api) if data end |
#inline_query ⇒ Object
69 70 71 72 |
# File 'lib/gruubY/context.rb', line 69 def inline_query data = update["inline_query"] InlineQuery.new(data, api: @api) if data end |
#managed_bot_updated ⇒ Object
139 140 141 142 |
# File 'lib/gruubY/context.rb', line 139 def managed_bot_updated data = @update["managed_bot_updated"] || @update["managed_bot"] wrap_type("ManagedBotUpdated", data) end |
#message ⇒ Object
17 18 19 20 |
# File 'lib/gruubY/context.rb', line 17 def data = Message.new(data, api: @api) if data end |
#message_reaction ⇒ Object
99 100 101 102 |
# File 'lib/gruubY/context.rb', line 99 def data = @update["message_reaction"] || @update["message_reaction_updated"] wrap_type("MessageReactionUpdated", data) end |
#message_reaction_count ⇒ Object
94 95 96 97 |
# File 'lib/gruubY/context.rb', line 94 def data = @update["message_reaction_count"] || @update["message_reaction_count_updated"] wrap_type("MessageReactionCountUpdated", data) end |
#poll(question, options) ⇒ Object
60 61 62 |
# File 'lib/gruubY/context.rb', line 60 def poll(question, ) @api.send_poll(chat_id, question, ) end |
#poll_update ⇒ Object
119 120 121 122 |
# File 'lib/gruubY/context.rb', line 119 def poll_update data = @update["poll"] wrap_type("Poll", data) end |
#pre_checkout_query ⇒ Object
74 75 76 77 |
# File 'lib/gruubY/context.rb', line 74 def pre_checkout_query data = update["pre_checkout_query"] PreCheckoutQuery.new(data, api: @api) if data end |
#purchased_paid_media ⇒ Object
124 125 126 127 |
# File 'lib/gruubY/context.rb', line 124 def purchased_paid_media data = @update["purchased_paid_media"] wrap_type("PurchasedPaidMedia", data) end |
#raw(method, params = {}) ⇒ Object
179 180 181 |
# File 'lib/gruubY/context.rb', line 179 def raw(method, params = {}) @api.raw(method, params) end |
#reply(text, keyboard = nil) ⇒ Object
164 165 166 167 168 |
# File 'lib/gruubY/context.rb', line 164 def reply(text, keyboard = nil) params = {} params[:reply_markup] = keyboard if keyboard @api.(chat_id, text, **params) end |
#session_data ⇒ Object
152 153 154 155 156 |
# File 'lib/gruubY/context.rb', line 152 def session_data return {} unless @session && user @session.get(user.id) end |
#set_session(value) ⇒ Object
158 159 160 161 162 |
# File 'lib/gruubY/context.rb', line 158 def set_session(value) return unless @session && user @session.set(user.id, value) end |
#shipping_query ⇒ Object
79 80 81 82 |
# File 'lib/gruubY/context.rb', line 79 def shipping_query data = update["shipping_query"] ShippingQuery.new(data, api: @api) if data end |
#story_update ⇒ Object
129 130 131 132 |
# File 'lib/gruubY/context.rb', line 129 def story_update data = @update["story"] Story.new(data, api: @api) if data end |
#text ⇒ Object
47 48 49 |
# File 'lib/gruubY/context.rb', line 47 def text &.dig("text") end |
#user ⇒ Object
148 149 150 |
# File 'lib/gruubY/context.rb', line 148 def user from end |