Method: Wechat::Responder::ClassMethods#on

Defined in:
lib/wechat/responder.rb

#on(message_type, with: nil, respond: nil, &block) ⇒ Object



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
# File 'lib/wechat/responder.rb', line 21

def on(message_type, with: nil, respond: nil, &block)
  raise 'Unknow message type' unless %i[text image voice video shortvideo link event click view scan batch_job location label_location fallback].include?(message_type)

  config = respond.nil? ? {} : { respond: respond }
  config[:proc] = block if block_given?

  if with.present?
    raise 'Only text, event, click, view, scan and batch_job can having :with parameters' unless %i[text event click view scan batch_job].include?(message_type)

    config[:with] = with
    if message_type == :scan
      raise 'on :scan only support string in parameter with, detail see https://github.com/Eric-Guo/wechat/issues/84' unless with.is_a?(String)

      self.known_scan_key_lists = with
    end
  elsif %i[click view scan batch_job].include?(message_type)
    raise 'Message type click, view, scan and batch_job must specify :with parameters'
  end

  case message_type
  when :click
    user_defined_click_responders(with) << config
  when :view
    user_defined_view_responders(with) << config
  when :batch_job
    user_defined_batch_job_responders(with) << config
  when :scan
    user_defined_scan_responders << config
  when :location
    user_defined_location_responders << config
  when :label_location
    user_defined_label_location_responders << config
  when :change_external_contact
    user_defined_change_external_contact_responders << config
  when :msgaudit_notify
    user_defined_msgaudit_notify_responders << config
  else
    user_defined_responders(message_type) << config
  end

  config
end