-
Notifications
You must be signed in to change notification settings - Fork 218
Webhooks & Account Activity Tutorial
This page will help you set up a Webhook server which on start will be registering to your webhooks and subscriptions.
In addition to that it will analyze any favourite tweet event performed by users registered and will add this information to the application console.
To use webhooks with Tweetinvi, you will need to use the following nuget package :
To develop the application you will need to access your machine from a public facing https url.
Different solutions offer this, but I have been using Ngrok and I liked its simplicity.
To use it, you will need to create an account, download the binary and execute the command ngrok http 8080
.
This operation will give you a list of public url endpoints that you can use to register a webhook on Twitter.
You will need to pick the Forwarding https
endpoint.
First of all, we need to create a HttpServer
that will be receive the webhooks calls from Twitter. To do so lets create a AspNetCore server.
In a new folder run the following commands :
dotnet new webapi
dotnet add package tweetinviapi
dotnet add package tweetinviapi.webhooksplugin
dotnet restore
Now lets clean the project to only keep what we need
rm -r Properties/ Controllers/ValuesController.cs appsettings.*
We can now create Controllers/TweetinviWebhookController.cs
:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Tweetinvi;
[Route("tweetinvi/")]
public class TweetinviWebhookController : Controller
{
[HttpGet("")]
public string Get()
{
return "Hello World!";
}
}
If you are using ngrok http 8080
, we want our server to be available via the port 8080.
To do so lets update the Program.cs
and add the .UseUrls()
.
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://localhost:8080/");
}
}
At this point, if you start your server, http://localhost:8080/tweetinvi should return
Hello World!
In the Startup.cs
file we want to inform the server that Tweetinvi will handle some of the requests received.
First lets add the namespaces we will need :
using Tweetinvi;
using Tweetinvi.AspNet;
using Tweetinvi.Core.Public.Models.Authentication;
Now lets modify Configure(IApplicationBuilder app, IHostingEnvironment env)
:
public static WebhookConfiguration WebhookConfiguration { get; set; }
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
Plugins.Add<WebhooksPlugin>();
WebhookConfiguration = new WebhookConfiguration(new ConsumerOnlyCredentials("CONSUMER_TOKEN", "CONSUMER_SECRET")
{
ApplicationOnlyBearerToken = "BEARER_TOKEN"
});
app.UseTweetinviWebhooks(WebhookConfiguration);
app.UseMvc();
}
Note : As you can see we need ConsumerOnly credentials to work with Twitter Webhooks. If you have not created an
Bearer Token
, I suggest you have a look at this wiki page.You can generate your bearer token just once and save it using the following code
var appCreds = Auth.SetApplicationOnlyCredentials("CONSUMER_KEY", "CONSUMER_SECRET", true);
var bearerToken = appCreds.ApplicationOnlyBearerToken;
At this point your server will respond to Twitter CRC challenge and this will enable you to register webhooks to attach to your server.
Our server is ready to handle the CRC challenge but we need to open an endpoint for this.
CRC challenge cannot be performed with Application Only credentials. It requires specific user credentials (learn more about authentication).
I will let you implement your logic to retrieve this information but lets create a placeholder in a new file CredentialsRetriever.cs
.
using System.Threading.Tasks;
using Tweetinvi.Models;
namespace Examplinvi.WebhooksServer
{
public static class CredentialsRetriever
{
public static async Task<ITwitterCredentials> GetUserCredentials(string userId)
{
// Implement your own logic for user credentials data retrieval
return await Task.FromResult(new TwitterCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET"));
}
}
}
... MORE TO COME, PLEASE BE PATIENT!