Class: Kogno::Telegram::Notification

Inherits:
Notification show all
Defined in:
lib/core/lib/telegram/notification.rb

Instance Method Summary collapse

Methods inherited from Notification

#carousel, #confirm, #delete_messages, #destroy_message_log, #document, #export_messages, #get_psid_from_response_log, #html_template, #import_messages, #initialize, #list, #loading_typing_off, #loading_typing_on, #message_log, #messenger_generic_template, #payload, rand_text, #raw, #recurring_notification_request, #render_html_template, #replace_place_holders, #response_log, #scheduled, #send_multiple, #send_private_reply, #send_using_token, #set_context, #set_typed_postbacks, #set_vars, #template, #typing, #typing_off, #whatsapp_template

Constructor Details

This class inherits a constructor from Kogno::Notification

Instance Method Details

#answer_callback_query(params = {}, callback_query_id = nil) ⇒ Object



67
68
69
70
# File 'lib/core/lib/telegram/notification.rb', line 67

def answer_callback_query(params={},callback_query_id=nil)
  callback_query_id = @message.callback_query_id if callback_query_id.nil?
   Api::answer_callback_query(callback_query_id, params)
end

#answer_inline_query(inline_query_id, delete = true) ⇒ Object



62
63
64
65
# File 'lib/core/lib/telegram/notification.rb', line 62

def answer_inline_query(inline_query_id, delete=true)
  @response_log << Api::answer_inline_query(inline_query_id, @inline_query_results)
  @inline_query_results = [] if delete
end

#button(text, buttons, extra_settings = {}) ⇒ Object



148
149
150
151
152
# File 'lib/core/lib/telegram/notification.rb', line 148

def button(text,buttons,extra_settings={})
  settings = {push_message: false, one_time_keyboard: true}.merge(extra_settings) # defaults
  buttons = buttons.replace_keys({payload: :callback_data, title: :text})
  self.push_message(self.keyboard(text,buttons,settings))
end

#contact(params) ⇒ Object



190
191
192
# File 'lib/core/lib/telegram/notification.rb', line 190

def contact(params)
  self.push_message(params, :contact)        
end

#contact_request(text, button_text = "📝") ⇒ Object



186
187
188
# File 'lib/core/lib/telegram/notification.rb', line 186

def contact_request(text, button_text="📝")
  self.push_message(Api::keyboard(text, [{text: button_text, request_contact: true}]))
end

#get_matched_message_id(id) ⇒ Object



57
58
59
60
# File 'lib/core/lib/telegram/notification.rb', line 57

def get_matched_message_id(id)
  return false unless @recipient.type == :user
  @recipient.matched_messages.find(id).platform_message_id 
end

#html(code, reply_markup = {}, extra_params = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/core/lib/telegram/notification.rb', line 81

def html(code, reply_markup = {} ,extra_params={})
  extra_params[:disable_web_page_preview] = !extra_params[:preview_url] unless extra_params[:preview_url].nil?
  extra_params = {disable_web_page_preview: true}.merge(extra_params)  
  params = {
    text: code,
    parse_mode: "HTML"
  }.merge(self.class.format_to_reply_markup(reply_markup)).merge(extra_params)
  self.push_message(params, :message)
end

#image(params) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/core/lib/telegram/notification.rb', line 198

def image(params)

  message = {
    photo: params[:url],
    caption: params[:caption]
  }
  unless params[:buttons].nil?
    replies = params[:buttons].replace_keys({payload: :callback_data, title: :text})
    replies = self.class.array_of_arrays(replies)
    message = message.merge(Api::add_inline_keyboard_markup(replies))
  end
  self.push_message(message, :photo)

end

#inline_keyboard(text, replies, extra_settings = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/core/lib/telegram/notification.rb', line 105

def inline_keyboard(text,replies, extra_settings={})
  settings = {typed_postbacks: Kogno::Application.config.typed_postbacks, push_message: true}.merge(extra_settings) # defaults
  text = t(text) if text.class == Symbol
  text = Notification::rand_text(text)
  begin
    set_typed_postbacks(replies.map{|reply|
      [reply[:text].to_payload, reply[:callback_data]] unless reply[:text].nil?
    }.compact.to_h) if settings[:typed_postbacks]
  rescue
    nil
  end
  
  replies = replies.each_slice(settings[:slice_replies]).to_a unless settings[:slice_replies].nil?
  result = Api::inline_keyboard_markup(text, replies, settings[:update])
  if settings[:push_message]
    self.push_message(result)
  else
    result
  end
end

#inline_query_result(type, results) ⇒ Object

      message_id: update_message_id,
      media: {
        type: :photo,
        caption: "<strong><a href=\"#{args[:url]\">#args[:title]</a></strong>\n<i>#args[:description]</i>",
        parse_mode: "HTML",
        media: args[:image]
      },
      reply_markup:{
        inline_keyboard: [
          [
            {
              text: args[:button_label],
              url: args[:url]
            }            
          ],
          pagination_buttons
        ]
      }           
    },
    :edit_media
  )        
end

end



373
374
375
376
377
378
379
380
381
# File 'lib/core/lib/telegram/notification.rb', line 373

def inline_query_result(type, results)
  body = self.push_inline_query_result(
    {
      type: type,
      id: @inline_query_results.count+1
    }.merge(results)
  )
  body
end

#keyboard(text, buttons, extra_settings = {}) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/core/lib/telegram/notification.rb', line 126

def keyboard(text,buttons, extra_settings={})
  settings = {typed_postbacks: Kogno::Application.config.typed_postbacks, push_message: true, one_time_keyboard: true}.merge(extra_settings) # defaults
  text = t(text) if text.class == Symbol
  text = Notification::rand_text(text)

  begin
    set_typed_postbacks(buttons.map{|reply|
      [reply[:text].to_payload, reply[:callback_data]] unless reply[:text].nil?
    }.compact.to_h) if settings[:typed_postbacks]
  rescue
    nil
  end

  buttons = buttons.each_slice(settings[:slice_replies]).to_a unless settings[:slice_replies].nil?
  result = Api::keyboard(text, buttons, settings[:one_time_keyboard])
  if settings[:push_message]
    self.push_message(result)
  else
    result
  end
end

#location(params) ⇒ Object



194
195
196
# File 'lib/core/lib/telegram/notification.rb', line 194

def location(params)
  self.push_message(params, :location)        
end

#location_request(text, button_text = "📍") ⇒ Object



182
183
184
# File 'lib/core/lib/telegram/notification.rb', line 182

def location_request(text, button_text="📍")
  self.push_message(Api::keyboard(text, [{text: button_text, request_location: true}]))
end

#markdown(syntax, reply_markup = {}, extra_params = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/core/lib/telegram/notification.rb', line 91

def markdown(syntax, reply_markup = {} ,extra_params={})
  extra_params[:disable_web_page_preview] = !extra_params[:preview_url] unless extra_params[:preview_url].nil?
  extra_params = {disable_web_page_preview: true}.merge(extra_params)  
  params = {
    text: syntax,
    parse_mode: "MarkdownV2"
  }.merge(self.class.format_to_reply_markup(reply_markup)).merge(extra_params)
  self.push_message(params, :message)
end

#match_message_ids(id, message_id) ⇒ Object



43
44
45
46
47
48
# File 'lib/core/lib/telegram/notification.rb', line 43

def match_message_ids(id, message_id)
  return false unless @recipient.type == :user
  matched_message = @recipient.matched_messages.find(id)
  matched_message.platform_message_id = message_id
  matched_message.save
end

#match_next_messageObject



50
51
52
53
54
55
# File 'lib/core/lib/telegram/notification.rb', line 50

def match_next_message()
  return false unless @recipient.type == :user
  matched_message = @recipient.matched_messages.create()
  @matched_message_id = matched_message.id
  return @matched_message_id
end

#quick_reply(text, replies, extra_settings = {}) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/core/lib/telegram/notification.rb', line 154

def quick_reply(text,replies,extra_settings={})
  settings = {typed_postbacks: true, push_message: false, editable: false}.merge(extra_settings) # defaults
  replies = [replies] if replies.class == Hash
  replies = replies.replace_keys({payload: :callback_data, title: :text})


  kgn_id = @message.get_kgn_id rescue nil
  if !kgn_id.nil?
    if kgn_id.class == String
        kogno_message_id = kgn_id.split("-")[1].to_i
        update_message_id = self.get_matched_message_id(kogno_message_id)
    elsif kgn_id.class == Integer
        update_message_id = kgn_id
    end      
  elsif settings[:updateable]
    kgn_id = "kgn-#{self.match_next_message}"
    replies = self.class.add_params_to_reply_payloads(replies, {kgn_id: kgn_id})
  end

  if update_message_id.nil?
    self.push_message(self.inline_keyboard(text,replies,settings))
  else
    replies = self.class.add_params_to_reply_payloads(replies, {kgn_id: update_message_id})
    self.push_message(self.inline_keyboard(text,replies,settings.merge({update: {message_id: update_message_id}})), :edit_text_message)      
  end   

end

#send(recipient_id = nil, delete = true) ⇒ Object



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
# File 'lib/core/lib/telegram/notification.rb', line 5

def send(recipient_id=nil,delete=true)    
  recipient_id = @recipient.chat_id if recipient_id.nil?
  messages = @before_messages+@messages+@after_messages
  @message_log = messages
  logger.write("\n\nSENDING MESSAGES...\n", :bright) unless messages.empty?
  messages.each do |message|
    case message[:type].to_sym 
      when :action
        sleep(message[:value][:duration]) if message[:value][:action].to_sym == :typing_on
        sent_response = nil
      when :photo
        sent_response = Api::send(recipient_id,message[:value], "sendPhoto")
      when :edit_media
        sent_response = Api::send(recipient_id,message[:value], "editMessageMedia")
      when :edit_text_message
        sent_response = Api::send(recipient_id,message[:value], "editMessageText")
      when :contact
        sent_response = Api::send(recipient_id,message[:value], "sendContact")
      when :location
        sent_response = Api::send(recipient_id,message[:value], "sendLocation")        
      when :message
        sent_response = Api::send(recipient_id,message[:value], "sendMessage")
      else #raw
        sent_response = Api::send(recipient_id,message[:value], message[:type])
    end 
    unless sent_response.nil?        
      if !@recipient.nil? and @recipient.type == :user
        if sent_response[:ok]
          @recipient.save
          match_message_ids(message[:matched_message_id], sent_response[:result][:message_id]) unless message[:matched_message_id].nil?
        end
      end
      @response_log << sent_response     
    end
  end    
  self.delete_messages() if delete
end

#text(text, extra_params = {}) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/core/lib/telegram/notification.rb', line 72

def text(text, extra_params={})
  extra_params[:disable_web_page_preview] = !extra_params[:preview_url] unless extra_params[:preview_url].nil?
  extra_params = {disable_web_page_preview: true}.merge(extra_params)  
  params = {
    text: text
  }.merge(extra_params)
  self.push_message(params, :message)
end

#typing_on(duration) ⇒ Object



101
102
103
# File 'lib/core/lib/telegram/notification.rb', line 101

def typing_on(duration)
  self.push_message({:action => :typing_on, :duration => duration}, :action)
end

#url(params) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/core/lib/telegram/notification.rb', line 229

def url(params)

  inline_keyboard = !params[:button].nil? ? [
    [
      {
        text: params[:button],
        url: params[:url]
      }            
    ]
  ] : []

  message = {
    caption: "<strong><a href=\"#{params[:url]}\">#{params[:title]}</a></strong>\n#{params[:sub_title]}",
    parse_mode: "HTML",
    photo: params[:image],
    reply_markup:{
      inline_keyboard: inline_keyboard
    }           
  }
  
  self.push_message(message,:photo)

end

#video(params) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/core/lib/telegram/notification.rb', line 214

def video(params)

  message = {
    video: params[:url],
    caption: params[:caption]
  }
  unless params[:buttons].nil?
    replies = params[:buttons].replace_keys({payload: :callback_data, title: :text})
    replies = self.class.array_of_arrays(replies)
    message = message.merge(Api::add_inline_keyboard_markup(replies))
  end
  self.push_message(message, "sendVideo")

end