This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.rb
142 lines (106 loc) · 3.03 KB
/
script.rb
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require "pry"
require "sinatra"
require "telegram/bot"
require "faraday"
require "httparty"
set :ssl_certificate, "cert.crt"
set :ssl_key, "pkey.pem"
set :port, 9000
CHANNEL_ID = ""
GROUP_ID = ""
CHANNEL_ID = ""
IMGUR_CLIENT_ID = ""
def cmd_search(keyword)
`xdotool mousemove 828 159 && \
xdotool click 1 && \
xdotool mousemove 828 200 && \
xdotool click 1 && \
xdotool type #{keyword} && \
xdotool key Return && \
xdotool mousemove 1355 183 && \
xdotool click 1`
end
def cmd_screenshot(filename)
`maim --quiet --delay=2 --format=png --geometry=628x255+812+625 ./images/#{filename}`
end
def process_and_send_message(message)
logger.info("MESSAGE: #{message}")
token = ENV["TOKEN"]
telegram_url = "https://api.telegram.org/bot#{token}"
puts telegram_url
msg = JSON.parse(message, object_class: OpenStruct)
text = msg&.message&.text
if text.nil?
logger.info("SKIP")
return
end
# Start checking
member = true
client = Telegram::Bot::Client.new(ENV["TOKEN"], timeout: 60)
begin
res1 = client.api.get_chat_member(chat_id: GROUP_ID, user_id: msg.message.from.id)
res2 = client.api.get_chat_member(chat_id: CHANNEL_ID, user_id: msg.message.from.id)
if res1["result"]["status"] == "left" && res2["result"]["status"] == "left"
raise "Kantoi"
end
rescue
member = false
client.api.send_message(
chat_id: CHANNEL_ID,
text: "INVALID MEMBER: #{msg.message.from.first_name} @#{msg.message.from.username}"
)
end
unless member
client.api.send_message(chat_id: msg.message.chat.id, text: "Hubungi @ultrasaham untuk akses :)")
return
end
# Checking end
chat_id = msg.message.chat.id
unless text.match?(/^[A-Za-z0-9-]+$/)
client = Telegram::Bot::Client.new(ENV["TOKEN"], timeout: 60)
# Ignore stupid messages
# client.api.send_message(chat_id: chat_id, text: "Not supported")
end
logger.info("SEARCH: #{text}")
# xrandr --output `xrandr | grep " connected" | cut -f1 -d""` --mode 1440x900
`change_res`
current_time = Time.now.strftime("%d-%m-%Y %I:%M:%S %p (%A)")
filename = "#{Time.now.to_i}-#{text.tr("&", "-").upcase}.png"
cmd_search(text)
cmd_screenshot(filename)
photo = Faraday::UploadIO.new("images/#{filename}", "image/png")
caption = "#{text.upcase} at #{current_time}"
logger.info("Sending...")
logger.info("Sending to user")
response = HTTParty.post(
"#{telegram_url}/sendPhoto",
body: {
chat_id: chat_id,
caption: caption,
photo: photo,
},
)
raise response.to_s unless response["ok"]
photo_file_id = response.dig("result", "photo", 1, "file_id")
logger.info("Sending to group")
response = HTTParty.post(
"#{telegram_url}/sendPhoto",
body: {
chat_id: CHANNEL_ID,
caption: caption,
photo: photo_file_id,
},
)
raise response.to_s unless response["ok"]
logger.info("Sent!")
rescue => error
logger.info(error)
end
# ENDPOINT
post "/search" do
process_and_send_message(request.body.read)
"OK"
end
get "/health" do
"OK"
end