-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp.js
73 lines (66 loc) · 1.8 KB
/
app.js
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
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
var request = require('request')
var path, node_ssh, ssh, fs
fs = require('fs')
path = require('path')
node_ssh = require('node-ssh')
ssh = new node_ssh()
app.use(bodyParser.json())
app.set('port', process.env.PORT || 4000)
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.get('/', function(req, res) {
res.send('Hello')
})
app.listen(app.get('port'), function() {
console.log('run at port', app.get('port'))
})
app.post('/webhook', (req, res) => {
var text = req.body.events[0].message.text
var sender = req.body.events[0].source.userId
var replyToken = req.body.events[0].replyToken
sendText(sender, text)
res.sendStatus(200)
})
function sendText(sender, text) {
ssh
.connect({
host: 'ec2-host.compute.amazonaws.com',
username: 'ec2-user',
privateKey: 'privatekey.pem'
})
.then((result, error) => {
ssh.execCommand(text, { cwd: '/var/www' }).then(function(result) {
console.log('STDOUT: ' + result.stdout)
console.log('STDERR: ' + result.stderr)
let data = {
to: sender,
messages: [
{
type: 'text',
text: result.stdout
}
]
}
request(
{
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer {AUTHEN KEY}'
},
url: 'https://api.line.me/v2/bot/message/push',
method: 'POST',
body: data,
json: true
},
function(err, res, body) {
if (err) console.log('error')
if (res) console.log('success')
if (body) console.log(body)
}
)
})
})
}