Module: Botomizer::Telegram
- Defined in:
- lib/botomizer/telegram.rb
Defined Under Namespace
Classes: Webhook
Constant Summary collapse
- API_URL =
'https://api.telegram.org/bot'.freeze
Class Attribute Summary collapse
-
.chat ⇒ Object
Returns the value of attribute chat.
-
.client ⇒ Object
Returns the value of attribute client.
-
.message ⇒ Object
Returns the value of attribute message.
-
.options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
- .delete_message(id = nil) ⇒ Object
- .parse_request(request) ⇒ Object
- .send(method, query = {}) ⇒ Object
- .send_message(text, options = {}) ⇒ Object
- .webhook ⇒ Object
Class Attribute Details
.chat ⇒ Object
Returns the value of attribute chat.
5 6 7 |
# File 'lib/botomizer/telegram.rb', line 5 def chat @chat end |
.client ⇒ Object
Returns the value of attribute client.
5 6 7 |
# File 'lib/botomizer/telegram.rb', line 5 def client @client end |
.message ⇒ Object
Returns the value of attribute message.
5 6 7 |
# File 'lib/botomizer/telegram.rb', line 5 def @message end |
.options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/botomizer/telegram.rb', line 5 def @options end |
Class Method Details
.delete_message(id = nil) ⇒ Object
59 60 61 |
# File 'lib/botomizer/telegram.rb', line 59 def (id = nil) send(:deleteMessage, {chat_id: @chat.id, message_id: id || @message.id}) end |
.parse_request(request) ⇒ Object
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 40 41 |
# File 'lib/botomizer/telegram.rb', line 11 def parse_request(request) = ['message', 'edited_message', 'channel_post', 'edited_channel_post', 'callback_query'].find do |key| request.has_key?(key) end unless .nil? if == 'callback_query' = request[]['message'] else = request[] end @message = Botomizer::Message.new({ id: ['message_id'], text: ['text'], data: request[]['data'] }, { name: @options[:name] }) @chat = Botomizer::Chat.new({ id: ['chat']['id'], type: ['chat']['type'], title: ['chat']['title'] || nil, first_name: ['chat']['first_name'] || nil, last_name: ['chat']['last_name'] || nil, username: ['chat']['username'] }) else raise Botomizer::BotError.new("Can't parse request") end end |
.send(method, query = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/botomizer/telegram.rb', line 43 def send(method, query = {}) response = HTTParty.post("#{API_URL}#{@options[:token]}/#{method}", {query: query}).parsed_response result = response['result'] || nil = result.kind_of?(Hash) && result.has_key?('message_id') ? result['message_id'].to_i : nil if response['ok'] {status: :success, message_id: , message: response['description'], result: result} else {status: :failed, error: response['error_code'], message: response['description']} end end |
.send_message(text, options = {}) ⇒ Object
55 56 57 |
# File 'lib/botomizer/telegram.rb', line 55 def (text, = {}) send(:sendMessage, {chat_id: @chat.id, text: text, parse_mode: 'HTML'}.merge()) end |