comparison mod_tweet_data/mod_tweet_data.lua @ 4597:c858c76d0845

mod_tweet_data: New module that fetches and sends tweet data based on tweet URLs in MUC messages.
author JC Brand <jc@opkode.com>
date Tue, 22 Jun 2021 11:41:16 +0200
parents
children efdc3e4dc5df
comparison
equal deleted inserted replaced
4596:c406e4bf7ee5 4597:c858c76d0845
1 local mod_muc = module:depends("muc")
2 local http = require "net.http"
3 local st = require "util.stanza"
4 local json = require "util.json"
5 local url_pattern = [[https://twitter.com/%S+/status/%S+]]
6 local xmlns_fasten = "urn:xmpp:fasten:0"
7 local xmlns_xhtml = "http://www.w3.org/1999/xhtml"
8 local twitter_apiv2_bearer_token = module:get_option_string("twitter_apiv2_bearer_token");
9
10 local function fetch_tweet_data(room, url, tweet_id, origin_id)
11 if not url then return; end
12 local options = {
13 method = "GET";
14 headers = { Authorization = "Bearer "..twitter_apiv2_bearer_token; };
15 };
16
17 http.request(
18 'https://api.twitter.com/2/tweets/'..tweet_id..'?expansions=author_id&tweet.fields=created_at,text&user.fields=id,name,username,profile_image_url',
19 options,
20 function(response_body, response_code, _)
21 if response_code ~= 200 then
22 module:log("debug", "Call to %s returned code %s and body %s", url, response_code, response_body)
23 return;
24 end
25
26 local response = json.decode(response_body);
27 if not response then return; end
28
29 local tweet = response['data'];
30 local author = response['includes']['users'][1];
31
32 local to = room.jid
33 local from = room and room.jid or module.host
34 local fastening = st.message({to = to, from = from, type = 'groupchat'}):tag("apply-to", {xmlns = xmlns_fasten, id = origin_id})
35
36 fastening:tag(
37 "meta",
38 {
39 xmlns = xmlns_xhtml,
40 property = 'og:article:author',
41 content = author['username']
42 }
43 ):up()
44
45 fastening:tag(
46 "meta",
47 {
48 xmlns = xmlns_xhtml,
49 property = 'og:article:published_time',
50 content = tweet['created_at']
51 }
52 ):up()
53
54 fastening:tag(
55 "meta",
56 {
57 xmlns = xmlns_xhtml,
58 property = 'og:description',
59 content = tweet['text']
60 }
61 ):up()
62
63 fastening:tag(
64 "meta",
65 {
66 xmlns = xmlns_xhtml,
67 property = 'og:image',
68 content = author['profile_image_url']
69 }
70 ):up()
71
72 fastening:tag(
73 "meta",
74 {
75 xmlns = xmlns_xhtml,
76 property = 'og:title',
77 content = author['username']
78 }
79 ):up()
80
81 fastening:tag(
82 "meta",
83 {
84 xmlns = xmlns_xhtml,
85 property = 'og:type',
86 content = 'tweet'
87 }
88 ):up()
89 fastening:tag(
90 "meta",
91 {
92 xmlns = xmlns_xhtml,
93 property = 'og:url',
94 content = 'https://twitter.com/'..author['username']..'/status/'..tweet['id']
95 }
96 ):up()
97
98 mod_muc.get_room_from_jid(room.jid):broadcast_message(fastening)
99 module:log("debug", tostring(fastening))
100 end
101 )
102 end
103
104 local function tweet_handler(event)
105 local room, stanza = event.room, st.clone(event.stanza)
106 local body = stanza:get_child_text("body")
107
108 if not body then return; end
109
110 local origin_id = stanza:find("{urn:xmpp:sid:0}origin-id@id")
111 if not origin_id then return; end
112
113 for url in body:gmatch(url_pattern) do
114 local _, _, _, tweet_id = string.find(url, "https://twitter.com/(%S+)/status/(%S+)");
115 fetch_tweet_data(room, url, tweet_id, origin_id);
116 end
117 end
118
119 module:hook("muc-occupant-groupchat", tweet_handler)
120
121
122 module:hook("muc-message-is-historic", function (event)
123 local fastening = event.stanza:get_child('apply-to', xmlns_fasten)
124 if fastening and fastening:get_child('meta', xmlns_xhtml) then
125 return true
126 end
127 end);