作者 | SHA1 | メッセージ | 日付 |
---|---|---|---|
James Peret | da1c692b9f | 7 年 前 | |
James Peret | ec42960e5b | 7 年 前 | |
James Peret | e77b06990b | 7 年 前 | |
James Peret | 0f1a817c61 | 7 年 前 |
@@ -1,6 +1,6 @@ |
||
1 | 1 |
{ |
2 | 2 |
"name": "hubot-multi-adapter", |
3 |
- "version": "0.3.7", |
|
3 |
+ "version": "0.3.8", |
|
4 | 4 |
"description": "A Hubot adapter that can receive messages thru socket.io or telegram", |
5 | 5 |
"main": "src/multi-adapter", |
6 | 6 |
"scripts": { |
@@ -11,9 +11,11 @@ |
||
11 | 11 |
"dependencies": { |
12 | 12 |
"body-parser": "^1.17.1", |
13 | 13 |
"express": "^4.15.2", |
14 |
+ "morgan": "^1.8.2", |
|
14 | 15 |
"node-telegram-bot-api": "^0.27.1", |
15 | 16 |
"parent-require": "^1.0.0", |
16 |
- "socket.io": "^1.4.5" |
|
17 |
+ "socket.io": "^1.4.5", |
|
18 |
+ "winston": "^2.3.1" |
|
17 | 19 |
}, |
18 | 20 |
"peerDependencies": { |
19 | 21 |
"hubot": ">=2.0" |
@@ -0,0 +1,44 @@ |
||
1 |
+var winston = require('winston'); |
|
2 |
+winston.emitErrs = true; |
|
3 |
+winston.setLevels(winston.config.syslog.levels); |
|
4 |
+ |
|
5 |
+var winstonLog = new winston.Logger({ |
|
6 |
+ transports: [ |
|
7 |
+ new winston.transports.File({ |
|
8 |
+ level: 'info', |
|
9 |
+ filename: 'beta-log.log', |
|
10 |
+ handleExceptions: true, |
|
11 |
+ json: true, |
|
12 |
+ maxsize: 5242880, //5MB |
|
13 |
+ maxFiles: 5, |
|
14 |
+ colorize: false |
|
15 |
+ }), |
|
16 |
+ new winston.transports.Console({ |
|
17 |
+ level: 'debug', |
|
18 |
+ handleExceptions: true, |
|
19 |
+ json: false, |
|
20 |
+ colorize: true |
|
21 |
+ }) |
|
22 |
+ ], |
|
23 |
+ exitOnError: false |
|
24 |
+}); |
|
25 |
+ |
|
26 |
+ |
|
27 |
+ |
|
28 |
+var log = function(robot, level, msg, data){ |
|
29 |
+ //console.log("logging data...") |
|
30 |
+ winstonLog.log(level, msg, data); |
|
31 |
+ //socket = robot.adapter.sockets[Object.keys(robot.adapter.sockets)[0]] |
|
32 |
+ var sockets = robot.adapter.sockets |
|
33 |
+ if(sockets){ |
|
34 |
+ for(socket_name in sockets) { |
|
35 |
+ if(sockets.hasOwnProperty(socket_name)) { |
|
36 |
+ var socket = sockets[socket_name]; |
|
37 |
+ socket.emit('log', { message: msg, data: data, level: level}) |
|
38 |
+ } |
|
39 |
+ } |
|
40 |
+ } |
|
41 |
+} |
|
42 |
+ |
|
43 |
+module.exports = log; |
|
44 |
+module.exports.winston = winstonLog; |
@@ -4,19 +4,22 @@ catch |
||
4 | 4 |
prequire = require('parent-require') |
5 | 5 |
{Robot,Adapter,TextMessage,User} = prequire 'hubot' |
6 | 6 |
|
7 |
-socket_port = parseInt process.env.HUBOT_SOCKETIO_PORT or 9090 |
|
8 |
-io = require('socket.io').listen socket_port |
|
9 |
-console.log("socket.io server on port " + socket_port); |
|
10 |
- |
|
7 |
+logger = require("./logger") |
|
11 | 8 |
express = require('express') |
12 | 9 |
app = express() |
13 | 10 |
bodyParser = require("body-parser") |
14 |
-app.use(bodyParser.urlencoded({ extended: false })) |
|
11 |
+app.use(bodyParser.urlencoded({ extended: true })) |
|
15 | 12 |
app.use(bodyParser.json()); |
13 |
+app.use(require('morgan')("default", { "stream": logger.stream })); |
|
16 | 14 |
|
17 | 15 |
http_port = parseInt process.env.HUBOT_HTTP_PORT or 80 |
18 | 16 |
app.listen http_port, => |
19 |
- console.log('HTTP server on port ' + http_port) |
|
17 |
+ #console.log('HTTP server on port ' + http_port) |
|
18 |
+ logger.winston.info("app listening on port " + http_port + ".") |
|
19 |
+ |
|
20 |
+socket_port = parseInt process.env.HUBOT_SOCKETIO_PORT or 9090 |
|
21 |
+io = require('socket.io').listen socket_port |
|
22 |
+logger.winston.info("socket.io server on port " + socket_port); |
|
20 | 23 |
|
21 | 24 |
TelegramBot = require('node-telegram-bot-api') |
22 | 25 |
#Telegram bot token (given when you create a new bot using the BotFather); |
@@ -29,9 +32,8 @@ class MultiAdapter extends Adapter |
||
29 | 32 |
super @robot |
30 | 33 |
|
31 | 34 |
send: (user, strings...) -> |
32 |
- console.log("Sending response to user " + user.user.name + " thru " + user.user.service + ":") |
|
33 |
- console.log(str for str in strings) |
|
34 |
- console.log(user) |
|
35 |
+ for str in strings |
|
36 |
+ logger(@robot, "info", "Sending response to user " + user.user.name + " thru " + user.user.service + ": " + str, { message: str, user: user } ) |
|
35 | 37 |
if user.user.service == "telegram" |
36 | 38 |
chatId = user.user.room; |
37 | 39 |
for str in strings |
@@ -46,37 +48,51 @@ class MultiAdapter extends Adapter |
||
46 | 48 |
for str in strings |
47 | 49 |
socket.emit 'message', str |
48 | 50 |
|
51 |
+ log: (log_msg) -> |
|
52 |
+ for socket in @sockets |
|
53 |
+ socket.emit 'message', log_msg |
|
54 |
+ |
|
49 | 55 |
run: -> |
50 | 56 |
io.sockets.on 'connection', (socket) => |
57 |
+ |
|
58 |
+ # logger.on 'logging', (transport, level, msg, meta) => |
|
59 |
+ # #console.log("> [%s] and [%s] have now been logged at [%s] to [%s]", msg, JSON.stringify(meta), level, transport.name) |
|
60 |
+ # socket.emit 'message', msg |
|
61 |
+ |
|
51 | 62 |
@sockets[socket.id] = socket |
52 |
- console.log("New user connect (" + socket.id + ")") |
|
63 |
+ #console.log("New user connect (" + socket.id + ")") |
|
64 |
+ logger(@robot, "info", "New user connect (" + socket.id + ")" ) |
|
53 | 65 |
@robot.brain.set 'log_id_' + socket.id, new Date().getUTCMilliseconds(); |
54 | 66 |
|
55 | 67 |
socket.on 'message', (data) => |
56 | 68 |
user = @userForId socket.id, name: data.username, room: socket.id |
57 |
- console.log("Message Received from user " + data.username + ":" ) |
|
58 |
- console.log(data.message) |
|
69 |
+ logger(@robot, "info", "Message Received from user " + data.username + " thru socket: " + data.message, { data: data } ) |
|
70 |
+ #console.log("Message Received from user " + data.username + ":" ) |
|
71 |
+ #console.log(data.message) |
|
59 | 72 |
user.name = data.username |
60 | 73 |
user.service = "socket" |
74 |
+ user.token = data.token |
|
61 | 75 |
@receive new TextMessage user, data.message |
62 | 76 |
|
63 | 77 |
socket.on 'disconnect', => |
64 |
- console.log("User disconected (" + socket.id + ")") |
|
78 |
+ #console.log("User disconected (" + socket.id + ")") |
|
79 |
+ logger(@robot, "info", "User disconected (" + socket.id + ")" ) |
|
65 | 80 |
@robot.brain.remove 'log_id_' + socket.id |
66 | 81 |
delete @sockets[socket.id] |
67 | 82 |
|
68 | 83 |
# Telegram Webhook |
69 | 84 |
app.post '/telegram-api', (req, res) => |
70 |
- console.log(req.body) |
|
71 |
- console.log(req.body.message.text) |
|
72 |
- chat_id = req.body.message.chat.id |
|
73 | 85 |
# Get username |
74 | 86 |
user_name = req.body.message.from.first_name + " " + req.body.message.from.last_name |
87 |
+ # Get text |
|
75 | 88 |
text = req.body.message.text |
89 |
+ # Get Chat ID |
|
90 |
+ chat_id = req.body.message.chat.id |
|
91 |
+ # Log Msg |
|
92 |
+ logger(@robot, "info", "Message Received from user #{user_name} (#{req.body.message.from.username}) thru Telegram: " + text, { data: req.body } ) |
|
93 |
+ # Set other things |
|
76 | 94 |
@robot.brain.set 'log_id_' + chat_id, new Date().getUTCMilliseconds(); |
77 | 95 |
user = @userForId chat_id, name: user_name, room: chat_id |
78 |
- console.log("Message Received from user " + user_name + ":" ) |
|
79 |
- console.log(text) |
|
80 | 96 |
user.service = "telegram" |
81 | 97 |
user.first_name = req.body.message.from.first_name |
82 | 98 |
user.last_name = req.body.message.from.last_name |
@@ -97,8 +113,7 @@ class MultiAdapter extends Adapter |
||
97 | 113 |
text = req.body.text |
98 | 114 |
@robot.brain.set 'log_id_' + chat_id, new Date().getUTCMilliseconds() |
99 | 115 |
user = @userForId chat_id, name: user_name, room: chat_id |
100 |
- console.log("Webhook received from " + user_name + " with command:" ) |
|
101 |
- console.log(text) |
|
116 |
+ logger(@robot, "info", "Webhook received from #{user_name} with command: " + text, { data: req.body } ) |
|
102 | 117 |
user.service = req.body.user.service |
103 | 118 |
user.first_name = req.body.user.first_name |
104 | 119 |
user.last_name = req.body.user.last_name |