Initial commit

This commit is contained in:
David A Roberts 2015-12-04 18:57:45 +10:00
commit 86fdbaf45c
3 changed files with 81 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

21
package.json Normal file
View File

@ -0,0 +1,21 @@
{
"name": "hubot-matrix",
"version": "1.0.0",
"description": "Matrix adapter for hubot",
"main": "src/matrix.coffee",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "David A Roberts",
"license": "MIT",
"dependencies": {
"parent-require": "^1.0.0",
"matrix-js-sdk": ">=0.3.0"
},
"peerDependencies": {
"hubot": ">=2.0"
},
"devDependencies": {
"coffee-script": ">=1.2.0"
}
}

59
src/matrix.coffee Normal file
View File

@ -0,0 +1,59 @@
try
{Robot,Adapter,TextMessage,User} = require 'hubot'
catch
prequire = require('parent-require')
{Robot,Adapter,TextMessage,User} = prequire 'hubot'
sdk = require 'matrix-js-sdk'
class Matrix extends Adapter
constructor: ->
super
@robot.logger.info "Constructor"
send: (envelope, strings...) ->
for str in strings
@robot.logger.info "Sending to #{envelope.room}: #{str}"
@client.sendTextMessage envelope.room, str
reply: (envelope, strings...) ->
for str in strings
@send envelope, "#{envelope.user.name}: #{str}"
run: ->
@robot.logger.info "Run #{@robot.name}"
client = sdk.createClient('https://matrix.org')
client.login 'm.login.password', {
user: @robot.name
password: process.env.HUBOT_MATRIX_PASSWORD
}, (err, data) =>
if err
@robot.logger.error err
return
@user_id = data.user_id
@access_token = data.access_token
@robot.logger.info "Logged in #{@user_id}"
@client = sdk.createClient
baseUrl: 'https://matrix.org'
accessToken: @access_token
userId: @user_id
@client.on 'syncComplete', =>
@robot.logger.info "Synced #{@client.getRooms().length} rooms"
@emit 'connected'
@client.on 'Room.timeline', (event, room, toStartOfTimeline) =>
if event.getType() == 'm.room.message' and toStartOfTimeline == false
message = event.getContent().body
name = event.getSender()
user = @robot.brain.userForId name
user.room = room.roomId
if user.name != @user_id
@robot.logger.info "Received message: '#{message}' in room: #{user.room}, from: #{user.name}."
@receive new TextMessage user, message
@client.on 'RoomMember.membership', (event, member) =>
if member.membership == 'invite' and member.userId == @user_id
@client.joinRoom(member.roomId).done =>
@robot.logger.info "Auto-joined #{member.roomId}"
@client.startClient 0
exports.use = (robot) ->
new Matrix robot