Module: RongCloud::Services::Message

Included in:
RongCloud::Service
Defined in:
lib/rong_cloud/services/message.rb,
lib/rong_cloud/services/message/message_channel.rb

Overview

Defined Under Namespace

Classes: MessageChannel

Instance Method Summary collapse

Instance Method Details

#send_broadcast_message(from_user_id, object_name, content, options = {}) ⇒ Object



81
82
83
# File 'lib/rong_cloud/services/message.rb', line 81

def send_broadcast_message(from_user_id, object_name, content, options = {})
  send_message(from_user_id, nil, :broadcast, object_name, content, options)
end

#send_chatroom_broadcast_message(from_user_id, object_name, content, options = {}) ⇒ Object



77
78
79
# File 'lib/rong_cloud/services/message.rb', line 77

def send_chatroom_broadcast_message(from_user_id, object_name, content, options = {})
  send_message(from_user_id, nil, :chatroom_broadcast, object_name, content, options)
end

#send_chatroom_message(from_user_id, to_chatroom_id, object_name, content, options = {}) ⇒ Object



73
74
75
# File 'lib/rong_cloud/services/message.rb', line 73

def send_chatroom_message(from_user_id, to_chatroom_id, object_name, content, options = {})
  send_message(from_user_id, to_chatroom_id, :chatroom, object_name, content, options)
end

#send_discussion_message(from_user_id, to_discussion_id, object_name, content, options = {}) ⇒ Object



69
70
71
# File 'lib/rong_cloud/services/message.rb', line 69

def send_discussion_message(from_user_id, to_discussion_id, object_name, content, options = {})
  send_message(from_user_id, to_discussion_id, :discussion, object_name, content, options)
end

#send_group_message(from_user_id, to_group_id, object_name, content, options = {}) ⇒ Object



65
66
67
# File 'lib/rong_cloud/services/message.rb', line 65

def send_group_message(from_user_id, to_group_id, object_name, content, options = {})
  send_message(from_user_id, to_group_id, :group, object_name, content, options)
end

#send_message(from_user_id, target_id, channel_name, object_name, content, *args) ⇒ Object

General method to send messages

Parameters:

  • from_user_id (String)

    sender id

  • target_id (String, Integer)

    receiver id,might be a user id, group id, discussion group id or chatroom id

  • channel_name (String)

    the message channel

  • object_name (String)

    message type name, could be built-in or custome names

  • content (Hash)

    Message object,view www.rongcloud.cn/docs/server.html#内置消息类型表 to learn more

  • args (Array)

    additional params,when it includes only one param, the args is a options; when 2 params, the first one is the ‘values` of a template message, the the last one is a options

  • options (Hash)

    options includes pushContent and pushData and so on, total supported options determined by the message type

Options Hash (content):

  • :content (Object)

    the body of message

  • :extra (Object)

    the extras of message,passed as a string and should be parsed by the message consumer

  • :user (Object)

    the informations for user, see: www.rongcloud.cn/docs/server.html#user_info



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rong_cloud/services/message.rb', line 28

def send_message(from_user_id, target_id, channel_name, object_name, content, *args)
  options = args.extract_options!
  values = args.first
  content_type = values.nil? ? :form_data : :json

  message_channel = MessageChannel.new(channel_name)
  params = { fromUserId: from_user_id, objectName: object_name, content: content.to_json }
  if message_channel.target_param_name
    params.merge!(message_channel.target_param_name => target_id)
  end
  if values
    if options[:pushContent].nil?
      raise RongCloud::MissingOptionError, "pushContent is required for template messages"
    end
    params.merge!(values: values)
  end

  params.merge!(options)
  request(message_channel.api_path, params, content_type)
end

#send_private_message(from_user_id, to_user_id, object_name, content, options = {}) ⇒ Object



49
50
51
# File 'lib/rong_cloud/services/message.rb', line 49

def send_private_message(from_user_id, to_user_id, object_name, content, options = {})
  send_message(from_user_id, to_user_id, :private, object_name, content, options)
end

#send_private_template_message(from_user_id, to_user_id, object_name, content, values, options = {}) ⇒ Object



53
54
55
# File 'lib/rong_cloud/services/message.rb', line 53

def send_private_template_message(from_user_id, to_user_id, object_name, content, values, options = {})
  send_message(from_user_id, to_user_id, :private_template, object_name, content, values, options)
end

#send_system_message(from_user_id, to_user_id, object_name, content, options = {}) ⇒ Object



57
58
59
# File 'lib/rong_cloud/services/message.rb', line 57

def send_system_message(from_user_id, to_user_id, object_name, content, options = {})
  send_message(from_user_id, to_user_id, :system, object_name, content, options)
end

#send_system_template_message(from_user_id, to_user_id, object_name, content, values, options = {}) ⇒ Object



61
62
63
# File 'lib/rong_cloud/services/message.rb', line 61

def send_system_template_message(from_user_id, to_user_id, object_name, content, values, options = {})
  send_message(from_user_id, to_user_id, :system_template, object_name, content, values, options)
end