def send_message(client_id, message):
    """Send a message to a channel.

  Args:
    client_id: The client id passed to create_channel.
    message: A string representing the message to send.

  Raises:
    InvalidChannelClientIdError: if client_id is not an instance of str or
        unicode, or if the (utf-8 encoded) string is longer than 64 characters.
    InvalidMessageError: if the message isn't a string or is too long.
    Errors returned by _ToChannelError
  """

    client_id = _ValidateClientId(client_id)

    if isinstance(message, unicode):
        message = message.encode('utf-8')
    elif not isinstance(message, str):
        raise InvalidMessageError

    if len(message) > MAXIMUM_MESSAGE_LENGTH:
        raise InvalidMessageError

    request = channel_service_pb.SendMessageRequest()
    response = api_base_pb.VoidProto()

    request.set_application_key(client_id)
    request.set_message(message)

    try:
        apiproxy_stub_map.MakeSyncCall(_GetService(), 'SendChannelMessage',
                                       request, response)
    except apiproxy_errors.ApplicationError, e:
        raise _ToChannelError(e)
Example #2
0
def send_message(application_key, message):
  """Send a message to a channel.

  Args:
    application_key: The key passed to create_channel.
    message: A string representing the message to send.

  Raises:
    Errors returned by _ToChannelError
  """
  request = channel_service_pb.SendMessageRequest()
  response = api_base_pb.VoidProto()

  request.set_application_key(application_key)
  request.set_message(message)

  try:
    apiproxy_stub_map.MakeSyncCall(_GetService(),
                                   'SendChannelMessage',
                                   request,
                                   response)
  except apiproxy_errors.ApplicationError, e:
    raise _ToChannelError(e)