Skip to content

Commit

Permalink
Merge pull request #6 from wsknorth/dev
Browse files Browse the repository at this point in the history
0.1.2
  • Loading branch information
Sabato Luca Guadagno authored Dec 6, 2017
2 parents 64de0e3 + 60ec4a7 commit a4f0cfd
Show file tree
Hide file tree
Showing 16 changed files with 435 additions and 1,033 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ENV=development
DEBUG=Midna:*
19 changes: 0 additions & 19 deletions Readme.md

This file was deleted.

1 change: 1 addition & 0 deletions Readme.md
File renamed without changes.
85 changes: 85 additions & 0 deletions docs/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<h1 align="center">
<img width="200px" src="/docs/images/midna.png_large"/>
<br/>midna
</h1>

<p align="center">
<a href="https://github.com/standard/standard"><img src="https://img.shields.io/badge/Code_style-Standard-green.svg?style=for-the-badge" alt="Standard"></a>
<a href="#"><img src="https://img.shields.io/badge/Version-0.1.2-blue.svg?style=for-the-badge" alt="Version"></a>
<a href="https://t.me/midnabot"><img src="https://img.shields.io/badge/Telegram-midnabot-blue.svg?style=for-the-badge" alt="Telegram"></a>
</p>

<br/>

## Sandbox - [gf3/sandbox](https://github.com/gf3/sandbox)

Execute JavaScript code
```
/sandbox ```console.log('Hello world')```
```

## Roll - [troygoode/node-roll](https://github.com/troygoode/node-roll)

Roll dices
```
/roll 2d10
```

## Development

Clone the repository
```bash
$ git clone https://github.com/wsknorth/midna.git && cd midna
```

Install dependencies
```bash
$ npm install
```

Edit the configuration file
```bash
$ cp lib/configs/configs.sample.json lib/configs/configs.json
$ vi lib/configs/configs.json
```

Run
```bash
$ npm start
```

## Production

Copy `midna.service` in `/etc/systemd/system`
```bash
$ sudo cp midna.service /etc/systemd/system/midna.service
```

Create midna user
```bash
$ sudo useradd -r -s /bin/bash -G midna midna
```

Enable
```bash
$ sudo systemctl enable midna
```

Start
```bash
$ sudo systemctl start midna
```

Stop
```bash
$ sudo systemctl stop midna
```

## Versioning
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/wsknorth/midna/tags).

## Author
Sabato Luca "[wsknorth](https://github.com/wsknorth)" Guadagno

## License
This project is licensed under the MIT License - see the [License](License.md) file for details
File renamed without changes.
19 changes: 0 additions & 19 deletions index.js

This file was deleted.

13 changes: 13 additions & 0 deletions lib/about/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const debug = require('debug')('Midna:about')

const { name, version, author, homepage } = require('../../package.json')

const about = () => (ctx) => {
const username = ctx.update.message.from.username
const text = ctx.update.message.text
debug(`${username}: ${text}`)

return ctx.replyWithMarkdown(`*${name} ${version}*\n${author}\n${homepage}`)
}

module.exports = { about }
12 changes: 10 additions & 2 deletions lib/configs/configs.sample.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"development": {
"username": "your bot username",
"token": "your bot token"
"username": "your bot username for development",
"token": "your bot token for development"
},

"production": {
"username": "your bot username for production",
"token": "your bot token for production",
"webhook": "your webhook for production",
"path": "your path for the webhook",
"port": 3000,
}
}
5 changes: 5 additions & 0 deletions lib/development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require('dotenv').load()

const { telegraf } = require('./telegraf')

telegraf.startPolling()
17 changes: 17 additions & 0 deletions lib/koa/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Koa = require('koa')
const body = require('koa-body')

const { configs } = require('../configs')
const { telegraf } = require('../telegraf')

const application = new Koa()

telegraf.telegram.setWebhook(configs.webhook)
application.use(body())
application.use((ctx, next) => {
return (ctx.method === 'POST' || ctx.url === configs.path)
? telegraf.handleUpdate(ctx.request.body, ctx.response)
: next()
})

module.exports = { koa: application }
6 changes: 6 additions & 0 deletions lib/production.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require('dotenv').load()

const { koa } = require('./koa')
const { configs } = require('./configs')

koa.listen(configs.port)
7 changes: 3 additions & 4 deletions lib/sandbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ const middleware = () => (ctx) => {
debug(`${username}: ${text}`)

const message = ctx.update.message
message.entities.map((entity) => {
if (entity.type === 'pre') {
const code = message.text.substring(entity.offset, entity.offset + entity.length)
message.entities.map((pre) => {
if (pre.type === 'pre') {
const code = message.text.substring(pre.offset, pre.offset + pre.length)
sandbox.run(code, (output) => {
debug(output)
let message = `@${username}\n\n` + '```\n'
message += `[ Code ]\n`
message += code
Expand Down
14 changes: 14 additions & 0 deletions lib/telegraf/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Telegraf = require('telegraf')

const { configs } = require('../configs')
const { sandbox } = require('../sandbox')
const { about } = require('../about')
const { roll } = require('../roll')

const bot = new Telegraf(configs.token, { username: configs.username })

bot.command('sandbox', sandbox())
bot.command('about', about())
bot.command('roll', roll())

module.exports = { telegraf: bot }
11 changes: 11 additions & 0 deletions midna.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Service]
ExecStart=/usr/bin/node /var/www/midna/lib/production.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=midna
User=midna
Group=midna

[Install]
WantedBy=multi-user.target
Loading

0 comments on commit a4f0cfd

Please sign in to comment.