Table of Contents | ||||
---|---|---|---|---|
|
Introducción
...
Introduction
The Platform's FlowEngine component offers a large number of connectors to work with social networks. Specifically, it includes two components: one to "listen" to what happens on Twitter (for example, receiving tweets about a hashtag, or doing a search) and another one to publish tweets:
...
En este post vamos a ver cómo usar estos componentes para ver en streaming lo que se dice en Twitter alrededor de un tema y posteriormente publicar un tuit con información recibida desde un servicio externo.
Paso 1: Crear APP en Portal Developer en Twitter
Lo primero que debemos hacer es crearnos una cuenta en Twitter. Una vez creada la cuenta iremos al Portal del Developer de Twitter en In this post, you will see how to use these components to:
Listen to what is being said on Twitter about a topic.
Store the information of a tweet in Platform.
Post a tweet with information received from an external service (OpenWeatherMaps).
Step 1: Create APP in Portal Developer on Twitter
The first thing you must do is create an account on Twitter. Once the account is created, go to the Twitter Developer Portal at https://developer.twitter.com/
...
Una vez dentro iremos a la sección Apps y dentro seleccionaremos Once there, go to the Apps section and once there, select Create an App:
...
Nos pedirá que confirmemos que queremos crear una cuenta de desarrollador y completar un conjunto de datos y tras completar nos llegará un mail para confirmar que queremos crear una cuenta de desarrollador:
...
Tras esto ya puedo crear mi App y conseguir las It will ask you to confirm that you want to create a developer account, then to complete a set of data. After completing those, you will receive an email to confirm that you want to create a developer account:
...
After this, you can create your App and get the Keys:
...
Como indican en la web debo guardar estas Keys ya que tendré que configurarlas en los componentes del FlowEngineAs indicated on the web, you must save these Keys, since you will have to configure them in the FlowEngine components:
...
Después de esto puedo probar el API REST de Twitter pulsando el botón After this, you can test the Twitter REST API by pressing the Test an endpoint .Como indican puedo probarlo con el comandobutton.
As indicated, you can test it with the command:
curl -X GET -H "Authorization: Bearer <BEARER TOKEN>" "https://api.twitter.com/2/tweets/440322224407314432"
...
Tras probar esto iré al Dashboard, dentro puedo configurar mi App
...
Iré a la sección Keys and tokens y seleccionaré After testing this, go to the Dashboard. There, you can configure your App.
...
Go to the Keys and tokens section and select Generate Access Token and Secret:
...
Que guardaré también junto con el API Key y el You must also save this, together with the API Key and the API Key Secret:
...
Hasta hace unos meses con esto he concluido la configuración necesaria en Twitter, pero con el cambio en la política de acceso a las APIs de Twitter al registrarnos lo haremos con el nivel Essential que no permite el acceso al API 1.1 que es la que usa el componente Node-Red del FlowEngine.
...
Tendremos que ir al Portal y solicitar el Apply for Elevated que ya nos da permiso para acceder al IMPORTANT: Until a few months ago, this was enough to finish the necessary configuration in Twitter; but, with the change in the access policy to the Twitter APIs, when registering, you must do it with the Essential level that does not allow access to API 1.1, which is the one that uses the Node-Red component of the FlowEngine.
...
This means that, for the communication with Twitter to work, you will have to go to the Portal and request the Apply for Elevated which will give you permission to access the API v1.1:
...
Os
pedirá una serie de información para habilitaros el acceso a este nivel, como el uso que queremos hacer, podemos indicar que queremos probar a recoger tuits con It will ask you for some information to enable access to this level, such as the use you want to make. You can indicate that you want to try to collect tweets with Node-red.
...
Step 2:
...
Configure Twitter
...
Component in Flowengine and retrieve and post Tweets
The first thing will be to have a user in the Platform instance that you want to use. For the example, you will use the Platform CloudLab environment (free environment for experimentation purposes). So, go to https://lab.onesaitplatform.com/ y si no tengo un usuario lo crearé con el botón SIGN UP. Es importante que cree un usuario tipo DEVELOPER and if you don't have a user, create it with the SIGN UP button. Bear in mind that you must create a DEVELOPER type user:
...
Con el usuario creado (recibiré un mail para validar la creación) ya puedo logarme en el Control Panel de PlataformaWith the user created (You will receive an email to validate the creation), you can now log in to the Platform Control Panel:
...
Una vez dentro iré a la opción de menú DEVELOPMENT>My Flows
...
Si es la primera vez que entro veré que no tengo creado un dominio del FlowEngine, así que pulsaré el botón + para crearlo:
...
Le daré un nombre y pulsaré Create:
...
Ahora ya veré mi dominio, que por defecto está parado, así que lo arrancaré primero:
...
Una vez arrancado puedo acceder a él con el botón de View It:
...
Finalmente accederé a la Paleta del FlowEngine, empezaré por seleccionar que no quiero crear un proyecto:
...
Puedo maximizar el área de trabajo con el botón de la esquina derecha:
...
Ahora buscaré los componentes de Twitter con el buscador y arrastraré el de Twitter In a la paleta:
...
El triángulo me indica que debo configurarlo, para esto pincho 2 veces sobre él para que aparezca la ventana de configuración.
...
En esta debo configurar los credenciales de Twitter que obtuvimos en el paso 1:
...
Después ya puedo configurar lo que quiero escuchar, por ejemplo puedo elegir una tendencia, en el ejemplo vamos a elegir #TheBatman en el día de su estreno en España:
...
Después configuraré el nodo debug para depurar por consola lo que llega:
...
Y para probarlo simplemente hago un DEPLOY:
...
Y ya veo en streaming los tuits sobre el tópico:
...
Para pararlo puedo eliminar el criterio de búsqueda.
Igualmente puedo publicar un tuit con el componente Twitter Out:
...
En el FlowEngine es muy fácil componer una lógica de negocio de modo que ante ciertos eventos (la llegada de un evento a una Entidad/Ontología por ejemplo) se componga un mensaje que se publique.
En este sencillo ejemplo simplemente publicaremos un texto, para eso usaremos el componente Inject configurado para inyectar un texto:
...
Tras esto haremos el despliegue y pulsaremos el botón del componente Inject.
Tras esto podremos ver el mensaje
...
Próximos pasos
En otro post posterior ampliaremos el uso de Twitter para crear un sencillo Bot que sea capaz de darnos la información meteorológica de la ciudad por la que preguntemos.
Once there, go to the DEVELOPMENT > My Flows menu option:
...
If this is the first time that you enter, you will see that you have not created a FlowEngine domain, so press the + button to create it:
...
Give it a name and click Create:
...
Now you can see your domain, which is stopped by default - so, firstly, start it:
...
Once started, you can access it with the View It button:
...
Finally, access the FlowEngine Palette. Start by selecting that you do not want to create a project:
...
You can maximize the work area with the button in the right corner:
...
Now, search for the Twitter components with the browser, and drag the Twitter In component to the canvas:
...
The triangle means that you must configure it. To do this, click twice on it so that the configuration window appears.
...
In it, you must configure the Twitter credentials that you had obtained in step 1:
...
Then, you can configure what you want to listen to. For example, you can choose a trend. In this example, you are going to choose #TheBatman on the day of its premiere in Spain:
...
Then, configure the debug node to debug the input through the console:
...
And to test it, simply do a DEPLOY:
...
And you will already be seeing, in streaming, the tweets on the topic:
...
To stop it, you can remove the search criteria.
You can also publish a tweet with the Twitter Out component:
...
In the FlowEngine, it is very easy to build a business logic so that, when certain events happen (the arrival of an event to an Entity/Ontology for example), a message is composed and published.
In this simple example, you will simply post a text. To do that, use the Inject component configured to inject a text:
...
After this, make the deployment and press the button in the Inject component.
After this, you can see the message:
...
Step 3: Store Tweet Information in a Platform Entity
If you go back to the example where we was listening to what was being said about The Batman:
...
and put a debug component configured to output the entire msg object:
...
then you will see, in the debug tab, a structure like this:
...
To insert this data in the Platform, you can create an Entity with these attributes: topic, payload, lang, tweet; or you can use the feature of the Platform to create an Entity from a JSON. This second way is much easier when you start from a JSON, as is this case.
From the Debug tab, select one of the objects with the Copy value button:
...
If you paste the content into an editor, you will see something like this:
...
Since you want to use this JSON as an example to create our Entity, you can remove the attributes inside the tweet, so you will have a JSON structure like this:
Code Block |
---|
{
"topic": "tweets/playlsit",
"payload": "robert pattinson as batman >>>>>>>",
"lang": "en",
"tweet": {},
"_msgid": "31a45aac.7fc556"
} |
Now go to the DEVELOPMENT/My Entities option in the Control Panel and select the + button to create a new Entity:
...
There, you will find several options. In this case, you are interested in the Create From File option to use the JSON as a basis to create your Entity:
...
When selecting it, you can copy the JSON that you already have as a template, then press the Generate JSON-Schema button:
...
Thenm click Create and complete the data of your Entity:
...
Once created, you can go back to the FlowEngine to continue with your flow.
Look for the onesaitplatform-insert component and drag it in, then configure it with your Entity:
...
If you connect it directly to the Twitter output and debug the output, you will see that you get an error:
...
This is because the output of the components in the FlowEngine is the msg.payload, which in your case is simply a text:
...
while the Insert node expects a complete JSON structure as you have defined, since you are interested in storing all the tweet info, including the tweeting user.
The way to solve this is to include a Function node between the two, like this:
...
where the Function node converts the msg to a payload on output:
...
If you now do the DEPLOY, you will see that the insertion is correct:
...
To verify that this information is really being stored in an Entity, access the Query Tool in TOOLS>Query Tool.
From there, you can query the stored data, just by selecting the Entity and configuring a query, or by using the default query:
...
Next steps
Once we have the data stored in the Platform, you can, for example:
Exploit the data with the Dashboard Engine, for example, showing tweets by zone, users who tweet the most about a topic, …
Create a Model with the Notebooks doing a sentiment analysis on the tweets, or a geographical distribution, …
Publish a REST API with the API Manager, with added information on a specific topic.