added eMessage Puppeteering Connector
parent
43115476f8
commit
ba841740fb
@ -0,0 +1,96 @@
|
||||
const Connector = require("./Connector")
|
||||
const config = require('../../config.json')
|
||||
const boskrypt = require('../../boskrypt')
|
||||
const md5 = require('md5')
|
||||
const axios = require('axios')
|
||||
const puppeteer = require('puppeteer')
|
||||
function sleep(ms) { return new Promise(r=>setTimeout(r, ms))}
|
||||
// [ "ecityruf", "123456789" ]
|
||||
class eMessagePuppeteerConnector extends Connector {
|
||||
constructor (amqpConnMngr) {
|
||||
super(amqpConnMngr)
|
||||
this.name = "em-puppet"
|
||||
this.duplexCapable = true
|
||||
this.supportBOSkrypt = true
|
||||
}
|
||||
async transmitMessage(msg, params) {
|
||||
const UUID = this.name+':'+md5(JSON.stringify([this.name,...params]))
|
||||
const target = params[0]
|
||||
if (target.split('#').length !== 2) throw 'No valid eMessage Parameter <serviceName#identifier>'
|
||||
const serviceName = target.split('#')[ 0 ], identifier = target.split('#')[ 1 ]
|
||||
|
||||
const $device = msg.routingParams.device
|
||||
let payloadBuffer = Buffer.from(msg.payload)
|
||||
const boskryptSupport = require("../DeviceRegistry").Devices[ $device ].supportBOSkrypt || false
|
||||
console.log('device supports boskrypt', boskryptSupport)
|
||||
if (boskryptSupport && !!config.pagers[ $device ] && config.pagers[ $device ].boskrypt.enabled) {
|
||||
//payloadBuffer
|
||||
const keyTable = config.pagers[ $device ].boskrypt.keys
|
||||
const FunkrufNummer = "em-"+identifier
|
||||
console.log('LUT ', FunkrufNummer, 'key=', keyTable[ FunkrufNummer ])
|
||||
if (!!keyTable[ FunkrufNummer ]) {
|
||||
payloadBuffer = Buffer.from(boskrypt.encrypt(payloadBuffer, keyTable[ FunkrufNummer ], 0))
|
||||
}
|
||||
}
|
||||
|
||||
let sendPromise = new Promise(async (res, rej) => {
|
||||
const browser = await puppeteer.launch({ headless: true })
|
||||
this.connectorRegistry.reportState(msg, UUID, 'puppettering')
|
||||
const page = await browser.newPage()
|
||||
await page.goto('https://ruf.emessage.de/')
|
||||
|
||||
const serviceSelect = 'select[name="pageSendForm:serviceSelect"]'
|
||||
await page.waitForSelector(serviceSelect)
|
||||
await page.select(serviceSelect, serviceName)
|
||||
await page.click(serviceSelect)
|
||||
await page.click(serviceSelect)
|
||||
|
||||
await sleep(1e3)
|
||||
|
||||
const selectServiceButton = 'input[name="pageSendForm:selectServiceButton"]'
|
||||
await page.waitForSelector(selectServiceButton)
|
||||
await page.click(selectServiceButton)
|
||||
|
||||
|
||||
const pagerNumberInput = 'input[name="pageSendForm:pagerNumberInput"]'
|
||||
await page.waitForSelector(pagerNumberInput)
|
||||
await page.type(pagerNumberInput, identifier)
|
||||
|
||||
const validatePagerNumberButton = 'input[id="pageSendForm:validatePagerNumberButton"]'
|
||||
await page.waitForSelector(validatePagerNumberButton)
|
||||
await page.click(validatePagerNumberButton)
|
||||
|
||||
const pageContentInput = 'textarea[id="pageSendForm:pageContentInput"]'
|
||||
await page.waitForSelector(pageContentInput)
|
||||
await page.type(pageContentInput, payloadBuffer.toString('ascii'))
|
||||
|
||||
const sendPageButton = 'input[id="pageSendForm:sendPageButton"]'
|
||||
await page.waitForSelector(sendPageButton)
|
||||
await page.click(sendPageButton)
|
||||
|
||||
const messageSentResult = 'span[id="pageSendForm:messageSentResult"]'
|
||||
await page.waitForSelector(messageSentResult)
|
||||
|
||||
let $messageSentResult = await page.$(messageSentResult)
|
||||
let value = await page.evaluate(el => el.textContent, $messageSentResult)
|
||||
// pageSendForm:messageSentResult
|
||||
if (value.indexOf("Meldung : OK") > -1) {
|
||||
this.connectorRegistry.reportState(msg, UUID, 'transit')
|
||||
await browser.close()
|
||||
return res(true)
|
||||
} else {
|
||||
this.connectorRegistry.reportState(msg, UUID, 'fail')
|
||||
await browser.close()
|
||||
return rej(value)
|
||||
}
|
||||
})
|
||||
sendPromise
|
||||
.catch((err) => {
|
||||
console.error(err)
|
||||
this.connectorRegistry.reportFail(msg, UUID)
|
||||
return false
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
module.exports = eMessagePuppeteerConnector
|
Loading…
Reference in New Issue