Skip to content

Instantly share code, notes, and snippets.

@substack substack/bot.js
Last active Nov 10, 2018

Embed
What would you like to do?
deltachat bot, not working yet
var DeltaChat = require('deltachat-node')
var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp')
var handle = require('./handle.js')
var minimist = require('minimist')
var argv = minimist(process.argv.slice(2), {
alias: { c: 'config' }
})
if (argv.config) {
readConfig(argv.config, function (err, config) {
if (err) error(err)
else start(config)
})
} else {
var configFile = path.join(
require('os').homedir(), '.config/faqbot/config.json'
)
mkdirp(path.basename(configFile), function (err) {
if (err) error(err)
else readConfig(configFile, function (err, config) {
if (err) error(err)
else start(config)
})
})
}
function readConfig (file, cb) {
fs.readFile(file, 'utf8', function (err, body) {
if (err && err.code === 'ENOENT') {
return promptConfig(file, cb)
} else if (err) {
return cb(err)
}
try { var config = JSON.parse(body) }
catch (err) { return cb(err) }
cb(null, config)
})
}
function promptConfig (configFile, cb) {
var config = {}
var prompts = [
{ prompt: 'bot email address', key: 'addr' },
{ prompt: 'email password', key: 'mail_pw', hide: true },
{
prompt: 'data path',
key: 'datadir',
default: path.join(require('os').homedir(),'.config/faqbot/data')
}
]
var n = 0
process.stdin.on('data', ondata)
var line = ''
showPrompt(prompts[n])
function ondata (buf) {
if (buf[0] === 0x03) {
process.stdin.setRawMode(false)
return process.exit(1)
}
line += buf.toString()
if (/[\r\n]/.test(line)) {
var parts = line.split(/[\r\n]/)
online(parts[0])
line = parts.slice(1).join('\n')
}
}
function online (line) {
var p = prompts[n]
if (!p) return save(config)
config[p.key] = line || p.default || ''
if (p.hide) process.stdout.write('\n')
p = prompts[++n]
if (p) showPrompt(p)
else save(config)
}
function showPrompt (p) {
if (p.hide) {
process.stdin.setRawMode(true)
} else {
process.stdin.setRawMode(false)
}
if (p.default) {
process.stdout.write(p.prompt + ' [' + p.default + ']: ')
} else {
process.stdout.write(p.prompt + ': ')
}
}
function save (config) {
process.stdin.setRawMode(false)
var str = JSON.stringify(config, null, 2)
mkdirp(path.dirname(configFile), function (err) {
if (err) return cb(err)
fs.writeFile(configFile, str, function (err) {
if (err) cb(err)
else cb(null, config)
})
})
}
}
function error (err) {
console.error(err)
process.exit(1)
}
function start (config) {
var dc = new DeltaChat
dc.open(config.datadir, function () {
if (!dc.isConfigured()) {
dc.once('ready', onready)
dc.configure(config)
} else {
onready()
}
function onready () {
dc.getChats().forEach(function (chid) {
console.log(dc.getFreshMessages(chid))
})
dc.on('DC_EVENT_IMAP_CONNECTED', function () {
console.log('imap connected')
})
dc.on('DC_EVENT_MSG_DELIVERED', function (chatId, msgId) {
var messages = dc.getFreshMessages()
messages.forEach(function (msg) {
handle(dc, msg)
})
})
}
})
}
module.exports = function (dc, msg) {
console.log(msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.