Class: Telegem::Core::Bot
- Inherits:
-
Object
- Object
- Telegem::Core::Bot
- Defined in:
- lib/core/bot.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#middleware ⇒ Object
readonly
Returns the value of attribute middleware.
-
#running ⇒ Object
readonly
Returns the value of attribute running.
-
#scenes ⇒ Object
readonly
Returns the value of attribute scenes.
-
#session_store ⇒ Object
readonly
Returns the value of attribute session_store.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #chat_boost(&block) ⇒ Object
- #chat_join_request(&block) ⇒ Object
- #command(name, **options, &block) ⇒ Object
- #contact(**options, &block) ⇒ Object
- #create_forum_topic(chat_id, name, **options) ⇒ Object
- #delete_webhook(&callback) ⇒ Object
- #error(&block) ⇒ Object
- #get_user_profile_audios(user_id, **options) ⇒ Object
- #get_webhook_info(&callback) ⇒ Object
- #hears(pattern, **options, &block) ⇒ Object
-
#initialize(token, **options) ⇒ Bot
constructor
A new instance of Bot.
- #location(&block) ⇒ Object
- #message_reaction(&block) ⇒ Object
- #message_reaction_count(&block) ⇒ Object
- #on(type, filters = {}, &block) ⇒ Object
- #poll_answer(&block) ⇒ Object
- #pre_checkout_query(&block) ⇒ Object
- #process(update_data) ⇒ Object
- #remove_my_profile_photo ⇒ Object
- #removed_chat_boost(&block) ⇒ Object
- #running? ⇒ Boolean
- #scene(id, &block) ⇒ Object
- #set_my_profile_photo(photo, **options) ⇒ Object
- #set_webhook(url, **options, &callback) ⇒ Object
- #shipping_query(&block) ⇒ Object
- #shutdown ⇒ Object
- #start_polling(**options) ⇒ Object
- #use(middleware, *args, &block) ⇒ Object
- #web_app_data(&block) ⇒ Object
- #webhook(app = nil, port: nil, host: '0.0.0.0', logger: nil, &block) ⇒ Object
Constructor Details
#initialize(token, **options) ⇒ Bot
Returns a new instance of Bot.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/core/bot.rb', line 10 def initialize(token, **) @token = token @api = API::Client.new(token, **.slice(:logger, :timeout)) @handlers = { message: [], callback_query: [], inline_query: [], chat_member: [], poll: [], pre_checkout_query: [], shipping_query: [], poll_answer: [], chat_join_request: [], chat_boost: [], removed_chat_boost: [], message_reaction: [], message_reaction_count: [] } @middleware = [] @scenes = {} @logger = [:logger] || Logger.new($stdout) @error_handler = nil @session_store = [:session_store] || Session::MemoryStore.new @running = false @offset = 0 @polling_options = .slice(:timeout, :limit, :allowed_updates) || {} end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
7 8 9 |
# File 'lib/core/bot.rb', line 7 def api @api end |
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
7 8 9 |
# File 'lib/core/bot.rb', line 7 def handlers @handlers end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'lib/core/bot.rb', line 7 def logger @logger end |
#middleware ⇒ Object (readonly)
Returns the value of attribute middleware.
7 8 9 |
# File 'lib/core/bot.rb', line 7 def middleware @middleware end |
#running ⇒ Object (readonly)
Returns the value of attribute running.
7 8 9 |
# File 'lib/core/bot.rb', line 7 def running @running end |
#scenes ⇒ Object (readonly)
Returns the value of attribute scenes.
7 8 9 |
# File 'lib/core/bot.rb', line 7 def scenes @scenes end |
#session_store ⇒ Object (readonly)
Returns the value of attribute session_store.
7 8 9 |
# File 'lib/core/bot.rb', line 7 def session_store @session_store end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
7 8 9 |
# File 'lib/core/bot.rb', line 7 def token @token end |
Instance Method Details
#chat_boost(&block) ⇒ Object
108 109 110 111 112 |
# File 'lib/core/bot.rb', line 108 def chat_boost(&block) on(:chat_boost) do |ctx| block.call(ctx) end end |
#chat_join_request(&block) ⇒ Object
102 103 104 105 106 |
# File 'lib/core/bot.rb', line 102 def chat_join_request(&block) on(:chat_join_request) do |ctx| block.call(ctx) end end |
#command(name, **options, &block) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/core/bot.rb', line 61 def command(name, **, &block) pattern = /^\/#{Regexp.escape(name)}(?:@\w+)?(?:\s+(.+))?$/i on(:message, text: pattern) do |ctx| ctx.match = ctx..text.match(pattern) ctx.state[:command_args] = ctx.match[1] if ctx.match block.call(ctx) end end |
#contact(**options, &block) ⇒ Object
78 79 80 81 82 |
# File 'lib/core/bot.rb', line 78 def contact(**, &block) on(:message, contact: true) do |ctx| block.call(ctx) end end |
#create_forum_topic(chat_id, name, **options) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/core/bot.rb', line 156 def create_forum_topic(chat_id, name, **) @api.call('createForumTopic', { chat_id: chat_id, name: name }.merge()) end |
#delete_webhook(&callback) ⇒ Object
205 206 207 208 209 210 211 |
# File 'lib/core/bot.rb', line 205 def delete_webhook(&callback) if callback @api.call!('deleteWebhook', {}, &callback) else @api.call('deleteWebhook', {}) end end |
#error(&block) ⇒ Object
177 178 179 |
# File 'lib/core/bot.rb', line 177 def error(&block) @error_handler = block end |
#get_user_profile_audios(user_id, **options) ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/core/bot.rb', line 148 def get_user_profile_audios(user_id, **) result = @api.call('getUserProfileAudios', { user_id: user_id }.merge()) return nil unless result && result['audios'] Types::UserProfileAudios.new(result) end |
#get_webhook_info(&callback) ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/core/bot.rb', line 213 def get_webhook_info(&callback) if callback @api.call!('getWebhookInfo', {}, &callback) else @api.call('getWebhookInfo', {}) end end |
#hears(pattern, **options, &block) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/core/bot.rb', line 71 def hears(pattern, **, &block) on(:message, text: pattern) do |ctx| ctx.match = ctx..text.match(pattern) block.call(ctx) end end |
#location(&block) ⇒ Object
162 163 164 165 166 |
# File 'lib/core/bot.rb', line 162 def location(&block) on(:message, location: true) do |ctx| block.call(ctx) end end |
#message_reaction(&block) ⇒ Object
120 121 122 123 124 |
# File 'lib/core/bot.rb', line 120 def (&block) on(:message_reaction) do |ctx| block.call(ctx) end end |
#message_reaction_count(&block) ⇒ Object
126 127 128 129 130 |
# File 'lib/core/bot.rb', line 126 def (&block) on(:message_reaction_count) do |ctx| block.call(ctx) end end |
#on(type, filters = {}, &block) ⇒ Object
168 169 170 |
# File 'lib/core/bot.rb', line 168 def on(type, filters = {}, &block) @handlers[type] << { filters: filters, handler: block } end |
#poll_answer(&block) ⇒ Object
84 85 86 87 88 |
# File 'lib/core/bot.rb', line 84 def poll_answer(&block) on(:poll_answer) do |ctx| block.call(ctx) end end |
#pre_checkout_query(&block) ⇒ Object
90 91 92 93 94 |
# File 'lib/core/bot.rb', line 90 def pre_checkout_query(&block) on(:pre_checkout_query) do |ctx| block.call(ctx) end end |
#process(update_data) ⇒ Object
221 222 223 224 |
# File 'lib/core/bot.rb', line 221 def process(update_data) update = Types::Update.new(update_data) process_update(update) end |
#remove_my_profile_photo ⇒ Object
144 145 146 |
# File 'lib/core/bot.rb', line 144 def remove_my_profile_photo @api.call('removeMyProfilePhoto', {}) end |
#removed_chat_boost(&block) ⇒ Object
114 115 116 117 118 |
# File 'lib/core/bot.rb', line 114 def removed_chat_boost(&block) on(:removed_chat_boost) do |ctx| block.call(ctx) end end |
#running? ⇒ Boolean
57 58 59 |
# File 'lib/core/bot.rb', line 57 def running? @running end |
#scene(id, &block) ⇒ Object
181 182 183 |
# File 'lib/core/bot.rb', line 181 def scene(id, &block) @scenes[id] = Scene.new(id, &block) end |
#set_my_profile_photo(photo, **options) ⇒ Object
138 139 140 141 142 |
# File 'lib/core/bot.rb', line 138 def set_my_profile_photo(photo, **) @api.call('setMyProfilePhoto', { photo: photo }.merge()) end |
#set_webhook(url, **options, &callback) ⇒ Object
197 198 199 200 201 202 203 |
# File 'lib/core/bot.rb', line 197 def set_webhook(url, **, &callback) if callback @api.call!('setWebhook', { url: url }.merge(), &callback) else @api.call('setWebhook', { url: url }.merge()) end end |
#shipping_query(&block) ⇒ Object
96 97 98 99 100 |
# File 'lib/core/bot.rb', line 96 def shipping_query(&block) on(:shipping_query) do |ctx| block.call(ctx) end end |
#shutdown ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/core/bot.rb', line 49 def shutdown return unless @running @logger.info "🛑 Shutting down bot..." @running = false sleep 0.1 end |
#start_polling(**options) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/core/bot.rb', line 41 def start_polling(**) @running = true @polling_options = Async do poll_loop end end |
#use(middleware, *args, &block) ⇒ Object
172 173 174 175 |
# File 'lib/core/bot.rb', line 172 def use(middleware, *args, &block) @middleware << [middleware, args, block] self end |
#web_app_data(&block) ⇒ Object
132 133 134 135 136 |
# File 'lib/core/bot.rb', line 132 def web_app_data(&block) on(:message, web_app_data: true) do |ctx| block.call(ctx) end end |
#webhook(app = nil, port: nil, host: '0.0.0.0', logger: nil, &block) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/core/bot.rb', line 185 def webhook(app = nil, port: nil, host: '0.0.0.0', logger: nil, &block) require_relative '../webhook/server' if block_given? Webhook::Server.new(self, &block) elsif app Webhook::Middleware.new(self, app) else Webhook::Server.new(self, port: port, host: host, logger: logger) end end |