Пример #1
0
        private void ObterToken(Entity tokenConfigRecord)
        {
            var client = new HttpClient();
            //get all attibutes required to get token
            string tokenEndPoint = (string)tokenConfigRecord[token_endpoint_attribute];
            string clientId      = (string)tokenConfigRecord[clientid_attribute];
            string clientSecret  = (string)tokenConfigRecord[client_secret_attribute];
            string resource      = (string)tokenConfigRecord[resource_attribute];

            client.BaseAddress = new Uri(tokenEndPoint);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair <string, string>("resource", resource),
                new KeyValuePair <string, string>("client_id", clientId),
                new KeyValuePair <string, string>("client_secret", clientSecret),
                new KeyValuePair <string, string>("client_info", "1"),
                new KeyValuePair <string, string>("grant_type", "client_credentials")
            });

            var    result        = client.PostAsync("", content).GetAwaiter().GetResult();
            string resultContent = result.Content.ReadAsStringAsync().GetAwaiter().GetResult();

            if (!string.IsNullOrEmpty(resultContent) && resultContent.Contains("access_token"))
            {
                var tokenParse = new TokenParse(resultContent);
                Token.Set(ExecutionContext, tokenParse.access_token);
                var dataExpiracao = DateTime.Now.AddSeconds(tokenParse.expires_in);
                ExpiresIn.Set(ExecutionContext, dataExpiracao);
                Sucess.Set(ExecutionContext, true);
                Message.Set(ExecutionContext, "Sucess");
                //grava token na entidade para cache
                var tokenUpdt = new Entity()
                {
                    LogicalName = tokenConfigRecord.LogicalName, Id = tokenConfigRecord.Id
                };
                tokenUpdt[data_expiracao] = dataExpiracao;
                tokenUpdt[valor_token]    = tokenParse.access_token;
                Service.Update(tokenUpdt);
            }
            else
            {
                if (!string.IsNullOrEmpty(resultContent) && resultContent.Contains("error_description"))
                {
                    var tokenParse = new TokenParse(resultContent);
                    Message.Set(ExecutionContext, tokenParse.error_description);
                }
                else
                {
                    Message.Set(ExecutionContext, "Requisição não retornou um token válido.");
                }
            }
        }
Пример #2
0
        protected override void SendMessage(NativeActivityContext context)
        {
            var         number  = base.From.Get(context);
            var         number2 = base.To.Get(context);
            var         text    = base.Body.Get(context);
            PhoneNumber from    = number;
            var         body    = text;

            var messageResource = MessageResource.Create(number2, null, from, null, body);

            Sid.Set(context, messageResource.Sid);
            Sucess.Set(context, value: true);
        }
Пример #3
0
        protected override void SendMessage(NativeActivityContext context)
        {
            var number   = base.From.Get(context);
            var number2  = base.To.Get(context);
            var text     = base.Body.Get(context);
            var list     = new List <Uri>(new Uri[] { new Uri(MediaUrl.Get(context)) });
            var from     = number;
            var body     = text;
            var mediaUrl = list;

            MessageResource messageResource = MessageResource.Create(number2, null, from, null, body, mediaUrl);

            Sid.Set(context, messageResource.Sid);
            Sucess.Set(context, value: true);
        }
Пример #4
0
        protected override void Execute(NativeActivityContext context)
        {
            try
            {
                Init(context);

                var text           = From.Get(context);
                var text2          = To.Get(context);
                var dateSentBefore = DateSentBefore.Get(context);
                var dateSent       = DateSent.Get(context);
                var dateSentAfter  = DateSentAfter.Get(context);
                var num            = Limit.Get(context);

                ReadMessageOptions readMessageOptions = new ReadMessageOptions();

                if (!string.IsNullOrWhiteSpace(text))
                {
                    readMessageOptions.From = text;
                }
                if (!string.IsNullOrWhiteSpace(text2))
                {
                    readMessageOptions.To = text2;
                }

                readMessageOptions.DateSentBefore = dateSentBefore;
                readMessageOptions.DateSent       = dateSent;
                readMessageOptions.DateSentAfter  = dateSentAfter;
                readMessageOptions.Limit          = ((num < 1) ? 1 : num);

                ResourceSet <MessageResource> resourceSet = MessageResource.Read(readMessageOptions);

                var dataTable = CriarDataTable();

                foreach (MessageResource item in resourceSet)
                {
                    dataTable.Rows.Add(item.DateCreated, item.DateUpdated, item.DateSent, item.Direction.ToString(), item.ErrorCode, item.ErrorMessage, item.From, item.Sid, item.Status.ToString(), item.To, item.Body);
                }
                Sucess.Set(context, value: true);
                Result.Set(context, dataTable);
            }
            catch (Exception ex)
            {
                Sucess.Set(context, value: false);
                ErrorMessage.Set(context, ex.Message);
            }
        }
Пример #5
0
        protected override void Execute(NativeActivityContext context)
        {
            try
            {
                Init(context);

                var flag = MessageResource.Delete(Sid.Get(context));

                Sucess.Set(context, flag);
                ErrorMessage.Set(context, flag ? "" : "Erro ao excluir mensagem.");
            }
            catch (Exception ex)
            {
                Sucess.Set(context, value: false);
                ErrorMessage.Set(context, ex.Message);
            }
        }
Пример #6
0
        protected override void Execute(NativeActivityContext context)
        {
            try
            {
                Init(context);

                var messageResource = MessageResource.Fetch(Sid.Get(context));

                Sucess.Set(context, true);
                DateCreated.Set(context, messageResource.DateCreated);
                DateSent.Set(context, messageResource.DateSent);
                Direction.Set(context, messageResource.Direction.ToString());
                From.Set(context, messageResource.From);
                Status.Set(context, messageResource.Status.ToString());
                To.Set(context, messageResource.To);
                Body.Set(context, messageResource.Body);
            }
            catch (Exception ex)
            {
                Sucess.Set(context, value: false);
                ErrorMessage.Set(context, ex.Message);
            }
        }
Пример #7
0
        protected override void OnExecute()
        {
            Trace("Start....");
            try
            {
                //get de record used to configure token creation
                EntityReference configRefence = TokenConfig.Get(ExecutionContext);
                if (configRefence == null)
                {
                    Sucess.Set(ExecutionContext, true);
                    Message.Set(ExecutionContext, "Nenhuma referencia de configuração foi definida na Action.");
                    return;
                }

                var tokenConfigRecord = Service.Retrieve(configRefence.LogicalName, configRefence.Id, new ColumnSet(atributos));
                ValidarCamposObrigatorios(tokenConfigRecord);
                var dataExpiracao = tokenConfigRecord.Contains(data_expiracao) ? tokenConfigRecord.GetAttributeValue <DateTime>(data_expiracao) : DateTime.MinValue;

                //Obtem novo token se atual expirado
                if (dataExpiracao < DateTime.Now)
                {
                    ObterToken(tokenConfigRecord);
                }
                else
                {
                    Token.Set(ExecutionContext, tokenConfigRecord.GetAttributeValue <string>(valor_token));
                    ExpiresIn.Set(ExecutionContext, tokenConfigRecord.GetAttributeValue <DateTime>(data_expiracao));
                    Sucess.Set(ExecutionContext, true);
                    Message.Set(ExecutionContext, "Sucess");
                }
            }
            catch (Exception ex)
            {
                Message.Set(ExecutionContext, ex.Message);
            }
        }