-
Notifications
You must be signed in to change notification settings - Fork 37
/
telegram_webhooks_controller.rb
105 lines (93 loc) · 2.79 KB
/
telegram_webhooks_controller.rb
1
2
3
4
5
6
7
8
9
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
class TelegramWebhooksController < Telegram::Bot::UpdatesController
include Telegram::Bot::UpdatesController::MessageContext
def start!(*)
respond_with :message, text: t('.content')
end
def help!(*)
respond_with :message, text: t('.content')
end
def memo!(*args)
if args.any?
session[:memo] = args.join(' ')
respond_with :message, text: t('.notice')
else
respond_with :message, text: t('.prompt')
save_context :memo!
end
end
def remind_me!(*)
to_remind = session.delete(:memo)
reply = to_remind || t('.nothing')
respond_with :message, text: reply
end
def keyboard!(value = nil, *)
if value
respond_with :message, text: t('.selected', value: value)
else
save_context :keyboard!
respond_with :message, text: t('.prompt'), reply_markup: {
keyboard: [t('.buttons')],
resize_keyboard: true,
one_time_keyboard: true,
selective: true,
}
end
end
def inline_keyboard!(*)
respond_with :message, text: t('.prompt'), reply_markup: {
inline_keyboard: [
[
{text: t('.alert'), callback_data: 'alert'},
{text: t('.no_alert'), callback_data: 'no_alert'},
],
[{text: t('.repo'), url: 'https://github.com/telegram-bot-rb/telegram-bot'}],
],
}
end
def callback_query(data)
if data == 'alert'
answer_callback_query t('.alert'), show_alert: true
else
answer_callback_query t('.no_alert')
end
end
def inline_query(query, _offset)
query = query.first(10) # it's just an example, don't use large queries.
t_description = t('.description')
t_content = t('.content')
results = Array.new(5) do |i|
{
type: :article,
title: "#{query}-#{i}",
id: "#{query}-#{i}",
description: "#{t_description} #{i}",
input_message_content: {
message_text: "#{t_content} #{i}",
},
}
end
answer_inline_query results
end
# As there is no chat id in such requests, we can not respond instantly.
# So we just save the result_id, and it's available then with `/last_chosen_inline_result`.
def chosen_inline_result(result_id, _query)
session[:last_chosen_inline_result] = result_id
end
def last_chosen_inline_result!(*)
result_id = session[:last_chosen_inline_result]
if result_id
respond_with :message, text: t('.selected', result_id: result_id)
else
respond_with :message, text: t('.prompt')
end
end
def message(message)
respond_with :message, text: t('.content', text: message['text'])
end
def action_missing(action, *_args)
if action_type == :command
respond_with :message,
text: t('telegram_webhooks.action_missing.command', command: action_options[:command])
end
end
end