/// <summary> /// Creates a message with attachments. /// </summary> /// <param name="recipient">PersonId, email or roomId of the message.</param> /// <param name="text">Text of the message.</param> /// <param name="attachments">List of attachments attached to the message.</param> /// <param name="messageType">Type of the message. It can be Text or Markdown.</param> /// <param name="target">Target for the message.</param> /// <param name="cancellationToken">A cancellation token for the task.</param> /// <returns>The created message id.</returns> public virtual async Task <string> CreateMessageWithAttachmentsAsync(string recipient, string text, IList <Attachment> attachments, MessageTextType messageType = MessageTextType.Text, MessageTarget target = MessageTarget.PersonId, CancellationToken cancellationToken = default) { Message result; var attachmentsContent = new List <object>(); foreach (var attach in attachments) { attachmentsContent.Add(attach.Content); } var request = new WebexMessageRequest { RoomId = target == MessageTarget.SpaceId ? recipient : null, ToPersonId = target == MessageTarget.SpaceId ? null : recipient, Text = text ?? string.Empty, Attachments = attachmentsContent.Count > 0 ? attachmentsContent : null, }; var http = (HttpWebRequest)WebRequest.Create(new Uri(MessageUrl)); http.PreAuthenticate = true; http.Headers.Add("Authorization", "Bearer " + Options.WebexAccessToken); http.Accept = "application/json"; http.ContentType = "application/json"; http.Method = "POST"; var parsedContent = JsonConvert.SerializeObject(request); var encoding = new ASCIIEncoding(); var bytes = encoding.GetBytes(parsedContent); var newStream = http.GetRequestStream(); newStream.Write(bytes, 0, bytes.Length); newStream.Close(); var response = await http.GetResponseAsync().ConfigureAwait(false); var stream = response.GetResponseStream(); using (var sr = new StreamReader(stream)) { var content = await sr.ReadToEndAsync().ConfigureAwait(false); result = JsonConvert.DeserializeObject <Message>(content); } return(result.Id); }
/// <summary> /// Wraps Webex API's CreateMessageAsync method. /// </summary> /// <param name="recipient">Target id of the message.</param> /// <param name="text">Text of the message.</param> /// <param name="files">List of files attached to the message.</param> /// <param name="messageType">Type of message. It can be Text or Markdown.</param> /// <param name="target">Target for the message.</param> /// <param name="cancellationToken">A cancellation token for the task.</param> /// <returns>The created message id.</returns> public virtual async Task <string> CreateMessageAsync(string recipient, string text, IList <Uri> files = null, MessageTextType messageType = MessageTextType.Text, MessageTarget target = MessageTarget.PersonId, CancellationToken cancellationToken = default) { var webexResponse = await _api.CreateMessageAsync(recipient, text, files, target, messageType, cancellationToken : cancellationToken).ConfigureAwait(false); return(webexResponse.Data.Id); }