Exemple #1
0
        public static Tuple <Message, ApiCallResponse> create(
            string access_token,
            string text,
            string channelId,
            List <string> receipientUsersnameOrIds,
            string reply_to = null,
            List <Annotation> annotations = null,
            Entities entities             = null,
            int machineOnly               = 0,
            bool?parse_links              = true,
            bool?parse_markdown_links     = true,
            List <File> toBeEmbeddedFiles = null)
        {
            ApiCallResponse apiCallResponse = new ApiCallResponse();
            Message         message         = new Message();

            try
            {
                if (string.IsNullOrEmpty(access_token))
                {
                    apiCallResponse.success      = false;
                    apiCallResponse.errorMessage = "Missing parameter access_token";
                    return(new Tuple <Message, ApiCallResponse>(message, apiCallResponse));
                }
                if (string.IsNullOrEmpty(text) && machineOnly == 0)
                {
                    apiCallResponse.success      = false;
                    apiCallResponse.errorMessage = "Missing parameter text";
                    return(new Tuple <Message, ApiCallResponse>(message, apiCallResponse));
                }
                if (channelId.ToLower() == "pm" && receipientUsersnameOrIds == null)
                {
                    apiCallResponse.success      = false;
                    apiCallResponse.errorMessage = "Missing parameter destinations for PM";
                    return(new Tuple <Message, ApiCallResponse>(message, apiCallResponse));
                }
                string requestUrl = Common.baseUrl;
                if (channelId.ToLower() == "pm")
                {
                    requestUrl += "/stream/0/channels/pm/messages";
                }
                else
                {
                    requestUrl += "/stream/0/channels/" + channelId + "/messages";
                }

                if (entities == null && (parse_links != null || parse_markdown_links != null))
                {
                    entities          = new Entities();
                    entities.hashtags = null;
                    entities.links    = null;
                    entities.mentions = null;
                }

                if (entities != null)
                {
                    entities.parse_links          = parse_links;
                    entities.parse_markdown_links = parse_markdown_links;
                }

                messageCreateParameters messageContent = new messageCreateParameters();
                messageContent.text         = text;
                messageContent.reply_to     = reply_to;
                messageContent.entities     = entities;
                messageContent.machine_only = machineOnly;
                messageContent.destinations = receipientUsersnameOrIds;

                if (toBeEmbeddedFiles != null)
                {
                    List <AppNetDotNet.Model.Annotations.AnnotationReplacement_File> files = new List <AppNetDotNet.Model.Annotations.AnnotationReplacement_File>();
                    if (annotations == null)
                    {
                        annotations = new List <Annotation>();
                    }
                    foreach (File file in toBeEmbeddedFiles)
                    {
                        AppNetDotNet.Model.Annotations.AnnotationReplacement_File fileReplacementAnnotation = new AppNetDotNet.Model.Annotations.AnnotationReplacement_File(file);
                        Annotation file_annotation = new Annotation(fileReplacementAnnotation.type, fileReplacementAnnotation.value);
                        annotations.Add(file_annotation);
                    }
                }

                List <Annotation.JSON_body> annotations_body = new List <Annotation.JSON_body>();
                if (annotations != null)
                {
                    if (annotations.Count > 0)
                    {
                        foreach (Annotation annotation in annotations)
                        {
                            annotations_body.Add(annotation.json_body);
                        }
                        messageContent.annotations = annotations_body;
                    }
                }

                JsonSerializerSettings settings = new JsonSerializerSettings();
                settings.NullValueHandling = NullValueHandling.Ignore;

                string jsonString = JsonConvert.SerializeObject(messageContent, Formatting.None, settings);
                if (toBeEmbeddedFiles != null)
                {
                    jsonString = jsonString.Replace("netAppCoreFile_dummy_for_replacement", "+net.app.core.file");
                }

                Dictionary <string, string> headers = new Dictionary <string, string>();
                headers.Add("Authorization", "Bearer " + access_token);
                Helper.Response response = Helper.SendPostRequestStringDataOnly(
                    requestUrl,
                    jsonString,
                    headers,
                    true,
                    contentType: "application/json");

                return(Helper.getData <Message>(response));
            }
            catch (Exception exp)
            {
                apiCallResponse.success          = false;
                apiCallResponse.errorMessage     = exp.Message;
                apiCallResponse.errorDescription = exp.StackTrace;
            }
            return(new Tuple <Message, ApiCallResponse>(message, apiCallResponse));
        }
        public static Tuple<Message, ApiCallResponse> create(string access_token, string text, string channelId, List<string> receipientUsersnameOrIds, string reply_to = null, List<Annotation> annotations = null, Entities entities = null, int machineOnly = 0)
        {
            ApiCallResponse apiCallResponse = new ApiCallResponse();
            Message message = new Message();
            try
            {
                if (string.IsNullOrEmpty(access_token))
                {
                    apiCallResponse.success = false;
                    apiCallResponse.errorMessage = "Missing parameter access_token";
                    return new Tuple<Message, ApiCallResponse>(message, apiCallResponse);
                }
                if (string.IsNullOrEmpty(text))
                {
                    apiCallResponse.success = false;
                    apiCallResponse.errorMessage = "Missing parameter text";
                    return new Tuple<Message, ApiCallResponse>(message, apiCallResponse);
                }
                if (channelId.ToLower() == "pm" && receipientUsersnameOrIds == null)
                {
                    apiCallResponse.success = false;
                    apiCallResponse.errorMessage = "Missing parameter destinations for PM";
                    return new Tuple<Message, ApiCallResponse>(message, apiCallResponse);
                }
                string requestUrl = Common.baseUrl;
                if (channelId.ToLower() == "pm")
                {
                    requestUrl += "/stream/0/channels/pm/messages";
                }
                else
                {
                    requestUrl += "/stream/0/channels/" + channelId + "/messages";
                }

                messageCreateParameters messageContent = new messageCreateParameters();
                messageContent.text = text;
                messageContent.reply_to = reply_to;
                //messageContent.annotations = annotations;
                messageContent.entities = entities;
                messageContent.machine_only = machineOnly;
                messageContent.destinations = receipientUsersnameOrIds;

                JsonSerializerSettings settings = new JsonSerializerSettings();
                settings.NullValueHandling = NullValueHandling.Ignore;

                string jsonString = JsonConvert.SerializeObject(messageContent, Formatting.None, settings);

                Dictionary<string, string> headers = new Dictionary<string, string>();
                headers.Add("Authorization", "Bearer " + access_token);
                Helper.Response response = Helper.SendPostRequestStringDataOnly(
                        requestUrl,
                        jsonString,
                        headers,
                        true,
                        contentType:"application/json");

                return Helper.getData<Message>(response);

            }
            catch (Exception exp)
            {
                apiCallResponse.success = false;
                apiCallResponse.errorMessage = exp.Message;
                apiCallResponse.errorDescription = exp.StackTrace;
            }
            return new Tuple<Message, ApiCallResponse>(message, apiCallResponse);
        }