-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNews-Feed.lua
85 lines (76 loc) · 1.83 KB
/
News-Feed.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
local json = require("json")
-- processId of the 0rbit process.
_0RBIT = "BaMK1dfayo75s3q1ow6AO64UDpD9SEFbeE8xYrY2fyQ"
_0RBT_TOKEN = "BUhZLMwQ6yZHguLtJYA5lLUa9LQzLXMXRfaq9FVcPJc"
-- Base URL for News API
URL = "https://saurav.tech/NewsAPI/top-headlines/category/health/in.json"
FEE_AMOUNT = "1000000000000" -- 1 $0RBT
NEWS = NEWS or {}
--[[
Function to send the latest news.
]]
function getNews(msg)
local news = json.encode(NEWS)
Handlers.utils.reply(news)(msg)
end
--[[
Function to fetch the news using the 0rbit.
]]
function fetchNews()
Send({
Target = _0RBT_TOKEN,
Action = "Transfer",
Recipient = _0RBIT,
Quantity = FEE_AMOUNT,
["X-Url"] = URL,
["X-Action"] = "Get-Real-Data"
})
print(Colors.green .. "GET Request sent to the 0rbit process.")
end
--[[
Function to update the news.
]]
function receiveData(msg)
local res = json.decode(msg.Data);
local articles;
local article;
if res.status == "ok" then
articles = res.articles;
for k, v in pairs(articles) do
article =
{
title = v.title,
description = v.description,
url = v.url
}
table.insert(NEWS, article)
end
print("News Updated")
else
print("Error in fetching news")
end
end
--[[
Handlers to get latest news.
]]
Handlers.add(
"GetNews",
Handlers.utils.hasMatchingTag("Action", "Get-News"),
getNews
)
--[[
Fetch News Handler to fetch the news using 0rbit.
]]
Handlers.add(
"FetchNews",
Handlers.utils.hasMatchingTag("Action", "Fetch-News"),
fetchNews
)
--[[
Handlers to receive data from the 0rbit process.
]]
Handlers.add(
"ReceiveData",
Handlers.utils.hasMatchingTag("Action", "Receive-Response"),
receiveData
)