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

Class Method Summary collapse

Class Attribute Details

.chatObject

Returns the value of attribute chat.



5
6
7
# File 'lib/botomizer/telegram.rb', line 5

def chat
  @chat
end

.clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/botomizer/telegram.rb', line 5

def client
  @client
end

.messageObject

Returns the value of attribute message.



5
6
7
# File 'lib/botomizer/telegram.rb', line 5

def message
  @message
end

.optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/botomizer/telegram.rb', line 5

def options
  @options
end

Class Method Details

.delete_message(id = nil) ⇒ Object



59
60
61
# File 'lib/botomizer/telegram.rb', line 59

def delete_message(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_key = ['message', 'edited_message', 'channel_post', 'edited_channel_post', 'callback_query'].find do |key|
    request.has_key?(key)
  end

  unless message_key.nil?
    if message_key == 'callback_query'
      message = request[message_key]['message']
    else
      message = request[message_key]
    end

    @message = Botomizer::Message.new({
        id: message['message_id'],
        text: message['text'],
        data: request[message_key]['data']
    }, {
      name: @options[:name]
    })
    @chat = Botomizer::Chat.new({
        id: message['chat']['id'],
        type: message['chat']['type'],
        title: message['chat']['title'] || nil,
        first_name: message['chat']['first_name'] || nil,
        last_name: message['chat']['last_name'] || nil,
        username: message['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
  message_id  = result.kind_of?(Hash) && result.has_key?('message_id') ? result['message_id'].to_i : nil

  if response['ok']
    {status: :success, message_id: 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 send_message(text, options = {})
  send(:sendMessage, {chat_id: @chat.id, text: text, parse_mode: 'HTML'}.merge(options))
end

.webhookObject



7
8
9
# File 'lib/botomizer/telegram.rb', line 7

def webhook
  Webhook.new(@client)
end