Create index.coffee
Browse files- index.coffee +91 -0
index.coffee
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
for err in ["unhandledRejection", "uncaughtException"]
|
2 |
+
process.on err, console.log
|
3 |
+
|
4 |
+
client = require "./client"
|
5 |
+
express = require "express"
|
6 |
+
fs = require "fs"
|
7 |
+
{ Events } = require "whatsapp-web.js"
|
8 |
+
rateLimit = require "express-rate-limit"
|
9 |
+
{ STATUS_CODES } = require "http"
|
10 |
+
{ tmpdir } = require "os"
|
11 |
+
v8 = require "v8"
|
12 |
+
|
13 |
+
{ GROUP_ID, PORT } = process.env
|
14 |
+
|
15 |
+
app = express()
|
16 |
+
app.set "json spaces", 4
|
17 |
+
app.set "trust proxy", ["loopback", "uniquelocal"]
|
18 |
+
app.use express.json()
|
19 |
+
app.use express.urlencoded extended: true
|
20 |
+
app.use "/file", express.static tmpdir()
|
21 |
+
|
22 |
+
app.use (req, res, next) ->
|
23 |
+
time = new Date().toLocaleString "fr"
|
24 |
+
param = if req.method is "GET" then req.query else req.body
|
25 |
+
console.log "\n[#{time}] #{req.method}: #{req.url}\n", param
|
26 |
+
next()
|
27 |
+
|
28 |
+
app.all "/", (_, res) ->
|
29 |
+
res.json
|
30 |
+
data:
|
31 |
+
uptime: new Date(process.uptime() * 1e3).toUTCString().split(" ")[4],
|
32 |
+
memoryUsage: process.memoryUsage(),
|
33 |
+
v8HeapStats: v8.getHeapStatistics()
|
34 |
+
|
35 |
+
imagineLimiter = rateLimit
|
36 |
+
windowMs: 60 * 1000,
|
37 |
+
max: 20
|
38 |
+
|
39 |
+
app.all "/imagine", imagineLimiter, (req, res) ->
|
40 |
+
unless req.method is "GET" or req.method is "POST"
|
41 |
+
res.status(405).json
|
42 |
+
message: STATUS_CODES["405"]
|
43 |
+
|
44 |
+
try
|
45 |
+
{ prompt } = if req.method is "GET" then req.query else req.body
|
46 |
+
unless prompt
|
47 |
+
res.status(400).json
|
48 |
+
message: STATUS_CODES["400"]
|
49 |
+
|
50 |
+
botId = ["13135550002", "@c.us"] # Meta AI Bot
|
51 |
+
filePath = "#{tmpdir()}/#{Math.random().toString(36).slice(2)}.jpg"
|
52 |
+
|
53 |
+
await client.sendMessage GROUP_ID, "@#{botId[0]} imagine #{prompt}",
|
54 |
+
mentions: [botId.join ""],
|
55 |
+
invokedBotWid: botId.join ""
|
56 |
+
|
57 |
+
client.on Events.MESSAGE_CREATE, (msg) ->
|
58 |
+
# console.log msg.from, msg.author, msg.type
|
59 |
+
if msg.from is GROUP_ID and msg.author is botId.join ""
|
60 |
+
# console.log msg
|
61 |
+
if msg.type is "image"
|
62 |
+
image = await msg.downloadMedia()
|
63 |
+
image = Buffer.from image.data, "base64"
|
64 |
+
fs.writeFile filePath, image, (e) ->
|
65 |
+
unless e
|
66 |
+
client.removeAllListeners Events.MESSAGE_CREATE
|
67 |
+
filePath = filePath.replace tmpdir(), "file"
|
68 |
+
fileUrl = "https://#{req.hostname}/#{filePath}"
|
69 |
+
res.redirect fileUrl
|
70 |
+
else
|
71 |
+
res.status(500).json
|
72 |
+
message: String e
|
73 |
+
|
74 |
+
else
|
75 |
+
text = msg.body
|
76 |
+
client.removeAllListeners Events.MESSAGE_CREATE
|
77 |
+
res.status(400).json
|
78 |
+
message: text
|
79 |
+
|
80 |
+
catch e
|
81 |
+
console.log e
|
82 |
+
e = String e
|
83 |
+
text = if e.startsWith "[object " then STATUS_CODES["500"] else e
|
84 |
+
res.status(500).json
|
85 |
+
message: text
|
86 |
+
|
87 |
+
setImmediate () ->
|
88 |
+
await client.initialize()
|
89 |
+
|
90 |
+
process.nextTick () ->
|
91 |
+
app.listen PORT or 7860
|