· 4 years ago · Mar 15, 2021, 06:38 PM
1tArgs = { ... }
2
3function configWrite()
4 local file = fs.open("/twitter.cfg", "w")
5 file.write(textutils.serialize(key))
6 file.close()
7end
8
9function configRead()
10 local file = fs.open("file.ltn", "r")
11 local config = textutils.unserialize(file.readAll())
12 file.close()
13 if config.key.type == "normal" then
14 authentication = {Authorization = "OAuth", oauth_consumer_key = key[1], oauth_consumer_secret = key[2], oauth_token = key[3], oauth_token_secret = key[4]}
15 elseif config.key.type == "bearer" then
16 authentication = {Authorization = "Bearer " .. key[1] .. ""}
17 end
18end
19
20function argsLogin() --The code that makes the login thing work.
21 if tArgs[2] == "1" or tArgs[2] == nil then
22 local key = {}
23 key.type = "normal"
24 local keyNames = {
25 "Consumer Key",
26 "Consumer Key Secret",
27 "Access Token",
28 "Access Token Secret"
29 }
30 for i = 1, 4 do
31 write("Input " .. keyNames[i] .. ": ")
32 key[i] = read("*")
33 end
34
35 configWrite()
36 elseif tArgs[2] == "2" then
37 local key = {}
38 key.type = "bearer"
39 write("Input Bearer Token: ")
40 key[1] = read("*")
41
42 configWrite()
43 else
44 printError("twitter <login> [oauth type]")
45 end
46end
47
48function argsTweet()
49 configRead()
50 if config.key.type == "normal" then
51 print("Message:")
52 message = read()
53 http.post("https://api.twitter.com/1.1/statuses/update.json?status=" .. textutils.urlEncode("" .. message .. "\nDebug mode on:\nHOST: " .. _HOST .. "") .. "")
54 elseif config.key.type == "bearer" then
55 printError("This requires OAuth 1.0a, you are using OAuth 2.0a")
56 end
57end
58
59if tArgs[1] == "login" then
60 argsLogin()
61elseif fs.exists("/twitter.cfg") == false then --If config file does not exist, send message telling you that it doesn't.
62 printError("Use \"twitter login\" to add your tokens.")
63elseif tArgs[1] == "tweet" then
64 argsTweet()
65end