- INFO
-
Para poder replicar esta funcionalidad necesitarás una cuenta en Linkedin y obtener un token. Puedes consultar el post que subí al respecto 2022/gmail-linkedin.html
Continuando con el post anterior de cómo publicar un toot en tu instancia del Fediverso cuando tienes un artículo nuevo en tu blog, esta vez vamos a hacer lo mismo pero notificandolo en Linkedin (ver jbake-toot.html)
Mi blog es un static site hecho con JBake usando Gradle y Asciidoctor así que básicamente es crear una tarea nueva en el buildSrc
buildSrc/src/main/groovy/LinkedinTask.groovy
import groovy.json.JsonOutput import groovy.xml.XmlSlurper import org.gradle.api.DefaultTask import org.gradle.api.provider.Property import org.gradle.api.tasks.Input import org.gradle.api.tasks.TaskAction import java.net.http.* abstract class LinkedinTask extends DefaultTask{ @Input abstract Property<String> getPublisher() @Input abstract Property<String> getToken() @Input abstract Property<String> getPostId() @TaskAction def runTask() { def xml = new XmlSlurper().parse("https://jorge.aguilera.soy/feed.xml") def entry = xml.entry.find{ "$it.id".endsWith( postId.get()+".html")} if( !entry ) { println("Post no encontrado") return } def hashtags = entry.category.collect{ "#"+it."@term"}.join(' ') def content = """ 🗣️ Nuevo post en mi blog $entry.title 📖 ${entry.summary} $hashtags ${entry.id} """ def json = JsonOutput.toJson([ "author": "urn:li:person:"+publisher.get(), "commentary": content, "visibility": "PUBLIC", "distribution": [ "feedDistribution": "MAIN_FEED", "targetEntities": [], "thirdPartyDistributionChannels": [], ], "lifecycleState": "PUBLISHED", "isReshareDisabledByAuthor": false ]) def client = HttpClient.newHttpClient() def request = HttpRequest.newBuilder() .header("Content-Type", "application/json; charset=UTF-8") .header("Authorization", "Bearer ${token.get()}") .header('X-Restli-Protocol-Version','2.0.0') .header('LinkedIn-Version','202308') .uri(URI.create('https://api.linkedin.com/rest/posts')) .POST(HttpRequest.BodyPublishers.ofString(json)) .build() def response = client.send(request, HttpResponse.BodyHandlers.ofString()) println response } }
y crear una task en el build.gradle
para llamarla en nuestro pipeline
build.gradle
tasks.register('linkedin', LinkedinTask){ publisher = findProperty("LINKEDIN_PUBLISHER") token = findProperty("LINKEDIN_TOKEN") postId = findProperty("postId") }
Los parámetros como el publisher or el token puedes guardarlos en el fichero $HOME/.gradle/gradle.properties y evitar así tener que recordarlos cada vez que quieras ejecutar la tarea.
El parámetro postId es simplemente el nombre corto (sin extension) del post. Por ejemplo en mi caso ejecutaría
./gradlew linkedin -P postId=jbake-linkedin
y se crearía una publicación en mi feed de Linkedin
Top comments (0)