Module: YATelegramBot::Base

Defined in:
lib/ya_telegram_bot/base.rb

Overview

extend your class with this module to use API. Or include it if you want to use in instances

Instance Method Summary collapse

Instance Method Details

#meObject



17
18
19
# File 'lib/ya_telegram_bot/base.rb', line 17

def me
  send_api_request 'getMe'
end

#send_message(params = {}) ⇒ Object

send text message to user.

required params:

  • :chat - id of chat

  • :text - your message

additional params:

  • :markdown - use telegram markdown



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ya_telegram_bot/base.rb', line 44

def send_message(params = {})
  fail NoChatSpecified unless params[:chat]
  fail NoMessageSpecified unless params[:text]

  params[:chat_id] = params[:chat]
  params.delete :chat

  if params[:markdown]
    params.delete :markdown
    params[:parse_mode] = 'Markdown'
  end

  response = send_api_request('sendMessage', params)
  response['ok']
end

#token(value) ⇒ Object



11
12
13
14
15
# File 'lib/ya_telegram_bot/base.rb', line 11

def token(value)
  @token = value
  @api_request_prefix = "bot#{@token}"
  @last_update_id = -1 # hmmm
end

#updatesObject

loads last updates. Note than after each #updates your @last_update_id will be changed so if you use it twice you won’t get same result



26
27
28
29
30
31
32
# File 'lib/ya_telegram_bot/base.rb', line 26

def updates
  updates = send_api_request 'getUpdates', offset: @last_update_id + 1
  fail ResponseIsNotOk unless updates['ok']

  @last_update_id = updates['result'].last['update_id'] unless updates['result'].empty?
  updates['result'].map { |u| u['message'] }
end