Esempio n. 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));
        }
Esempio n. 2
0
        public static Tuple <Post, ApiCallResponse> create(string access_token, string text, string reply_to = null, List <File> toBeEmbeddedFiles = null, List <Annotation> annotations = null, Entities entities = null, bool?parse_links = null)
        {
            ApiCallResponse apiCallResponse = new ApiCallResponse();
            Post            post            = new Post();

            try
            {
                if (string.IsNullOrEmpty(access_token))
                {
                    apiCallResponse.success      = false;
                    apiCallResponse.errorMessage = "Missing parameter access_token";
                    return(new Tuple <Post, ApiCallResponse>(post, apiCallResponse));
                }
                if (string.IsNullOrEmpty(text))
                {
                    apiCallResponse.success      = false;
                    apiCallResponse.errorMessage = "Missing parameter text";
                    return(new Tuple <Post, ApiCallResponse>(post, apiCallResponse));
                }
                string requestUrl = Common.baseUrl + "/posts";
                Dictionary <string, string> headers = new Dictionary <string, string>();
                headers.Add("Authorization", "Bearer " + access_token);

                postCreateParameters postCreateContent = new postCreateParameters();
                postCreateContent.text     = text;
                postCreateContent.reply_to = reply_to;
                //postCreateContent.entities = new EntitiesWithoutAllProperty(entities);
                //postCreateContent.annotations = annotations;

                /*if (postCreateContent.entities != null)
                 * {
                 *  postCreateContent.entities.parse_links = parse_links;
                 * }*/


                if (toBeEmbeddedFiles != null)
                {
                    if (postCreateContent.annotations == null)
                    {
                        postCreateContent.annotations = new List <AppNetDotNet.Model.Annotations.AnnotationReplacement_File>();
                    }
                    foreach (File file in toBeEmbeddedFiles)
                    {
                        AppNetDotNet.Model.Annotations.AnnotationReplacement_File fileReplacementAnnotation = new AppNetDotNet.Model.Annotations.AnnotationReplacement_File(file);
                        postCreateContent.annotations.Add(fileReplacementAnnotation);
                    }
                }

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

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

                jsonString = jsonString.Replace("netAppCoreFile_dummy_for_replacement", "+net.app.core.file");

                Helper.Response response;
                response = Helper.SendPostRequestStringDataOnly(
                    requestUrl,
                    jsonString,
                    headers,
                    true,
                    contentType: "application/json");

                return(Helper.getData <Post>(response));
            }
            catch (Exception exp)
            {
                apiCallResponse.success          = false;
                apiCallResponse.errorMessage     = exp.Message;
                apiCallResponse.errorDescription = exp.StackTrace;
            }
            return(new Tuple <Post, ApiCallResponse>(post, apiCallResponse));
        }
Esempio n. 3
0
            public static Tuple<Post,ApiCallResponse> create(string access_token, string text, string reply_to = null, List<File> toBeEmbeddedFiles = null, List<Annotation> annotations = null, Entities entities = null, int machine_only = 0)
            {
                ApiCallResponse apiCallResponse = new ApiCallResponse();
                Post post = new Post();
                try
                {
                    if (string.IsNullOrEmpty(access_token))
                    {
                        apiCallResponse.success = false;
                        apiCallResponse.errorMessage = "Missing parameter access_token";
                        return new Tuple<Post, ApiCallResponse>(post, apiCallResponse);
                    }
                    if (string.IsNullOrEmpty(text))
                    {
                        apiCallResponse.success = false;
                        apiCallResponse.errorMessage = "Missing parameter text";
                        return new Tuple<Post, ApiCallResponse>(post, apiCallResponse);
                    }
                    string requestUrl = Common.baseUrl + "/stream/0/posts";
                    Dictionary<string, string> headers = new Dictionary<string, string>();
                    headers.Add("Authorization", "Bearer " + access_token);

                    postCreateParameters postCreateContent = new postCreateParameters();
                    postCreateContent.text = text;
                    postCreateContent.reply_to = reply_to;
                    postCreateContent.machine_only = machine_only;
                    postCreateContent.entities = new EntitiesWithoutAllProperty(entities);
                    //postCreateContent.annotations = annotations;
                    if (toBeEmbeddedFiles != null)
                    {
                        if (postCreateContent.annotations == null)
                        {
                            postCreateContent.annotations = new List<AppNetDotNet.Model.Annotations.AnnotationReplacement_File>();
                        }
                        foreach (File file in toBeEmbeddedFiles)
                        {
                            AppNetDotNet.Model.Annotations.AnnotationReplacement_File fileReplacementAnnotation = new AppNetDotNet.Model.Annotations.AnnotationReplacement_File(file);
                            postCreateContent.annotations.Add(fileReplacementAnnotation);
                        }
                    }

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

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

                    jsonString = jsonString.Replace("netAppCoreFile_dummy_for_replacement", "+net.app.core.file");

                    Helper.Response response;
                        response = Helper.SendPostRequestStringDataOnly(
                            requestUrl,
                            jsonString,
                            headers,
                            true,
                            contentType: "application/json");

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