Class: TelegramOnSteroids::Params

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram_on_steroids/params.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Params

Returns a new instance of Params.



4
5
6
# File 'lib/telegram_on_steroids/params.rb', line 4

def initialize(params)
  @params = params
end

Instance Method Details

#[](key) ⇒ Object



8
9
10
# File 'lib/telegram_on_steroids/params.rb', line 8

def [](key)
  @params[key]
end

#callback?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/telegram_on_steroids/params.rb', line 70

def callback?
  !callback_data.nil?
end

#callback_dataObject



62
63
64
# File 'lib/telegram_on_steroids/params.rb', line 62

def callback_data
  @params.dig("callback_query", "data")
end

#chatObject



39
40
41
42
43
44
45
# File 'lib/telegram_on_steroids/params.rb', line 39

def chat
  @params.dig("message", "chat") ||
    @params.dig("callback_query", "message", "chat") ||
    @params.dig("edited_message", "chat") ||
    @params.dig("channel_post", "chat") ||
    @params.dig("edited_channel_post", "chat")
end

#chat_idObject



35
36
37
# File 'lib/telegram_on_steroids/params.rb', line 35

def chat_id
  chat&.dig("id")
end

#command?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/telegram_on_steroids/params.rb', line 78

def command?
  !!message_text&.start_with?("/")
end


82
83
84
85
# File 'lib/telegram_on_steroids/params.rb', line 82

def deep_link_payload
  match = /\A\/(startgroup|start) (?<payload>.+)\z/.match(message_text)
  match["payload"] if match
end

#inline_dataObject



66
67
68
# File 'lib/telegram_on_steroids/params.rb', line 66

def inline_data
  @params.dig("inline_query", "query")
end

#language_codeObject



23
24
25
# File 'lib/telegram_on_steroids/params.rb', line 23

def language_code
  user&.dig("language_code") || "en"
end

#messageObject



47
48
49
50
51
52
# File 'lib/telegram_on_steroids/params.rb', line 47

def message
  @params["message"] ||
    @params["edited_message"] ||
    @params["channel_post"] ||
    @params["edited_channel_post"]
end

#message_textObject



58
59
60
# File 'lib/telegram_on_steroids/params.rb', line 58

def message_text
  message&.dig("text")
end

#start?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/telegram_on_steroids/params.rb', line 74

def start?
  !!message_text&.start_with?("/start")
end

#to_hObject



87
88
89
# File 'lib/telegram_on_steroids/params.rb', line 87

def to_h
  @params
end

#typeObject



54
55
56
# File 'lib/telegram_on_steroids/params.rb', line 54

def type
  @type ||= (TelegramOnSteroids::UPDATE_TYPES & @params.keys).first
end

#userObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/telegram_on_steroids/params.rb', line 12

def user
  @user ||= message&.dig("from") ||
    @params.dig("callback_query", "from") ||
    @params.dig("pre_checkout_query", "from") ||
    @params.dig("shipping_query", "from") ||
    @params.dig("inline_query", "from") ||
    @params.dig("chosen_inline_result", "from") ||
    @params.dig("poll_answer", "user") ||
    @params.dig("chat_join_request", "from")
end

#user_idObject



27
28
29
# File 'lib/telegram_on_steroids/params.rb', line 27

def user_id
  user&.dig("id")
end

#usernameObject



31
32
33
# File 'lib/telegram_on_steroids/params.rb', line 31

def username
  user&.dig("username")
end