Пример #1
0
        private static void WebhookServerInitialization(IApplicationBuilder app, IConsumerOnlyCredentials consumerOnlyCredentials)
        {
            Plugins.Add <WebhooksPlugin>();

            WebhookConfiguration = new WebhookConfiguration(consumerOnlyCredentials);

            app.UseTweetinviWebhooks(WebhookConfiguration);
        }
Пример #2
0
        public async Task <IWebhookEnvironmentDTO[]> GetAllWebhooksAsync(IConsumerOnlyCredentials consumerCredentials)
        {
            var query  = "https://api.twitter.com/1.1/account_activity/all/webhooks.json";
            var result = _twitterAccessor.ExecuteQuery <IGetAllWebhooksResultDTO>(query, HttpMethod.GET, consumerCredentials, null);

            result?.Environments?.ForEach(environment =>
            {
                environment.ConsumerCredentials = consumerCredentials;
            });

            return(await Task.FromResult(result?.Environments));
        }
Пример #3
0
        /// <summary>
        /// Set the current thread credentials based on application only credentials.
        /// To execute http requests, application only credentials needs a bearer token.
        /// Setting  the initializeBearerToken to true will initialize your credentials so that they are ready to be used.
        /// </summary>
        public static ITwitterCredentials SetApplicationOnlyCredentials(IConsumerOnlyCredentials consumerOnlyCredentials, bool initializeBearerToken = false)
        {
            Credentials = new TwitterCredentials(consumerOnlyCredentials.ConsumerKey, consumerOnlyCredentials.ConsumerSecret);
            Credentials.ApplicationOnlyBearerToken = consumerOnlyCredentials.ApplicationOnlyBearerToken;

            if (initializeBearerToken)
            {
                InitializeApplicationOnlyCredentials(Credentials);
            }

            return(Credentials);
        }
Пример #4
0
        public async Task <IWebhookSubcriptionListDTO> GetListOfSubscriptionsAsync(
            string webhookEnvironmentName,
            IConsumerOnlyCredentials credentials)
        {
            var query = $"https://api.twitter.com/1.1/account_activity/all/{webhookEnvironmentName}/subscriptions/list.json";

            var result = _twitterAccessor.ExecuteQuery(query, HttpMethod.GET, credentials);

            var subscriptions = _jsonObjectConverter.DeserializeObject <IWebhookSubcriptionListDTO>(result.Text);

            return(await Task.FromResult(subscriptions));
        }
Пример #5
0
        public AccountActivityRequestHandler(
            IWebhookDispatcher dispatcher,
            IWebhooksRoutes routes,
            IWebhooksHelper webhooksHelper,
            IFactory <AccountActivityStream> accountActivityStreamFactory,
            ITwitterClient client)
        {
            _dispatcher = dispatcher;
            _accountActivityStreamFactory = accountActivityStreamFactory;
            _router = new WebhookRouter(dispatcher, routes, webhooksHelper);

            _consumerOnlyCredentials = new ConsumerOnlyCredentials(client.Credentials);
        }
Пример #6
0
        public async Task <bool> TryRouteRequestAsync(IWebhooksRequest request, IConsumerOnlyCredentials credentials)
        {
            var isCrcChallenge = _webhooksHelper.IsCrcChallenge(request);

            if (isCrcChallenge)
            {
                return(await _webhooksRoutes.TryToReplyToCrcChallengeAsync(request, credentials).ConfigureAwait(false));
            }

            var jsonBody = await request.GetJsonFromBodyAsync().ConfigureAwait(false);

            _webhookDispatcher.WebhookMessageReceived(new WebhookMessage(jsonBody));

            return(await Task.FromResult(true).ConfigureAwait(false));
        }
Пример #7
0
        private static async Task SubscribeToAllAccountActivities(
            IConsumerOnlyCredentials consumerOnlyCredentials,
            IWebhookEnvironmentDTO environment)
        {
            // If you wish to subscribe to the different account activity events you can do the following
            var subscriptions = await Webhooks.GetListOfSubscriptionsAsync(environment.Name, consumerOnlyCredentials);

            subscriptions.Subscriptions.ForEach(subscription =>
            {
                var activityStream = Stream.CreateAccountActivityStream(subscription.UserId);

                activityStream.JsonObjectReceived += (sender, args) => { Console.WriteLine("json received : " + args.Json); };

                WebhookConfiguration.AddActivityStream(activityStream);
            });
        }
Пример #8
0
        private static async Task RegisterAccountActivities(IConsumerOnlyCredentials consumerOnlyCredentials)
        {
            var webhookEnvironments = await Webhooks.GetAllWebhookEnvironmentsAsync(consumerOnlyCredentials);

            webhookEnvironments.ForEach(async environment =>
            {
                var webhookEnvironment = new RegistrableWebhookEnvironment(environment)
                {
                    Credentials = consumerOnlyCredentials
                };

                WebhookConfiguration.AddWebhookEnvironment(webhookEnvironment);

                await SubscribeToAllAccountActivities(consumerOnlyCredentials, environment);
            });
        }
Пример #9
0
        public async Task <IGetWebhookSubscriptionsCountResultDTO> CountNumberOfSubscriptionsAsync(
            IConsumerOnlyCredentials credentials)
        {
            var query = "https://api.twitter.com/1.1/account_activity/subscriptions/count.json";

            var result = _twitterAccessor.ExecuteQuery(query, HttpMethod.GET, credentials, null);

            if (result.StatusCode == 32)
            {
                return(null);
            }

            var subscriptionsCount = _jsonObjectConverter.DeserializeObject <IGetWebhookSubscriptionsCountResultDTO>(result.Text);

            return(await Task.FromResult(subscriptionsCount));
        }
Пример #10
0
        private async Task ReplyToCrcChallengeRequestAsync(string crcToken, IWebhooksRequest request, IConsumerOnlyCredentials credentials)
        {
            var crcResponseInfo = _webhooksHelper.CreateCrcResponseToken(crcToken, credentials.ConsumerSecret);

            await request.WriteInResponseAsync(crcResponseInfo.Json, crcResponseInfo.ContentType).ConfigureAwait(false);
        }
Пример #11
0
        public async Task <bool> TryToReplyToCrcChallengeAsync(IWebhooksRequest request, IConsumerOnlyCredentials credentials)
        {
            var crcToken = request.GetQuery()["crc_token"];

            if (crcToken.IsNullOrEmpty())
            {
                return(false);
            }

            await ReplyToCrcChallengeRequestAsync(crcToken[0], request, credentials).ConfigureAwait(false);

            return(true);
        }
Пример #12
0
 public T ExecuteQuery <T>(string query, HttpMethod method, IConsumerOnlyCredentials credentials,
                           HttpContent httpContent) where T : class
 {
     return(ExecuteQuery <T>(query, method, new TwitterCredentials(credentials), httpContent));
 }
Пример #13
0
 // Consumer Credentials
 public IWebRequestResult ExecuteQuery(string query, HttpMethod method,
                                       IConsumerOnlyCredentials credentials, HttpContent httpContent)
 {
     return(ExecuteQuery(query, method, new TwitterCredentials(credentials), httpContent));
 }
Пример #14
0
 public static async Task <IWebhookSubcriptionListDTO> GetListOfSubscriptionsAsync(string webhookEnvironmentName, IConsumerOnlyCredentials credentials)
 {
     return(await WebhookController.GetListOfSubscriptionsAsync(webhookEnvironmentName, credentials));
 }
Пример #15
0
 public static async Task <IGetWebhookSubscriptionsCountResultDTO> CountNumberOfSubscriptionsAsync(IConsumerOnlyCredentials consumerCredentials)
 {
     return(await WebhookController.CountNumberOfSubscriptionsAsync(consumerCredentials));
 }
Пример #16
0
 public WebhookConfiguration(IConsumerOnlyCredentials consumerOnlyCredentials) : this()
 {
     ConsumerOnlyCredentials = consumerOnlyCredentials;
 }
Пример #17
0
 public static async Task <IWebhookEnvironmentDTO[]> GetAllWebhookEnvironmentsAsync(IConsumerOnlyCredentials consumerCredentials)
 {
     return(await WebhookController.GetAllWebhooksAsync(consumerCredentials));
 }
Пример #18
0
 public static IWebRequestResult ExecuteConsumerQuery(string query, HttpMethod method, IConsumerOnlyCredentials credentials)
 {
     return(Accessor.ExecuteQuery(query, method, credentials, null));
 }