Skip to content

Commit

Permalink
Tidy up, library'ize
Browse files Browse the repository at this point in the history
  • Loading branch information
valgaze committed Sep 15, 2021
1 parent 623ee9d commit 1374d66
Show file tree
Hide file tree
Showing 29 changed files with 1,986 additions and 19,303 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ You can run the commands using npx or install globally (see below):
| :-------------------------: | :-------------------------------------------------------------------------------------------------- |
| `npx speedybot setup` | scaffold a starter speedybot project (currently requires git) |
| `npx speedybot setup 1234_tokenhere_4567` | scaffold a speedybot project using the value after setup as the token|

| `npx speedybot help` | show basic CLI help info |


Expand Down
80 changes: 79 additions & 1 deletion docs/how-to.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,82 @@ export const handlers = [{
},
helpText: 'A special handler for handling user input'
}]
```
```

## Send a message

```ts
speedybot.webex.messages.create({
toPersonEmail: '[email protected]',
markdown:"This text will be **bold**",
})

```

## Mention someone by email

```ts
<@personEmail:joe@bongo.com>
```

```ts
{
keyword: ['hello', 'hey', 'yo', 'watsup', 'hola'],
handler(bot, trigger) {
if (trigger.person.emails) {
const [email] = trigger.person.emails;
const mentionString = `Hey there, I'll **mention** you here, <@personEmail:${email}>`
bot.say(mentionString)
}
},
helpText: `A simple handler that greets the user`
}
```

## Send a message to a user

```ts
{
keyword: ['sendmessage'],
handler(bot, trigger) {
// 1) Send message back to room/person who triggered handler
bot.say(`Here's a reply message`)

// 2) send a direct message
bot.dm('[email protected]', 'message goes here')

// 2) Send message to a particular uer
bot.webex.messages.create({
toPersonEmail: '[email protected]',
text: `Here is a message sent right to you`
})
},
helpText: `Example handler to send a message`
}
```

## Deployment



**[expressjs](https://expressjs.com/en/5x/api.html) example

```ts


```

Koa Example

```
// todo
```

## Tunneling

**IMPORTANT:**

```sh
npx speedybot-tunnel
```

187 changes: 187 additions & 0 deletions docs/singlefile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
If you need a starter repo, see here: **[https://github.com/valgaze/speedybot-starter](https://github.com/valgaze/speedybot-starter)**

## Zero-config launch

Just provide a token & a list of handlers-- config looks like **[this](https://github.com/valgaze/speedybot/blob/master/settings/config.json)** & the handlers looks like **[this](https://github.com/valgaze/speedybot/blob/master/settings/handlers.ts)**

```sh
npm install speedybot webex-node-bot-framework
```

```ts
import { Launch } from 'speedybot'
import handlers from './settings/handlers'
import config from './settings/config.json'
// helper types
import { BotHandler, SpeedybotConfig } from 'speedybot'

async function boot(config: SpeedybotConfig, handlers: BotHandler[]) {
try {
const speedybotInst = await Launch(config, handlers)
const phrase = config.webhookUrl ? `Webhook available here: ${config.webhookUrl}` : 'Connected using websockets'
console.log(`Success! ${phrase}`)
return speedybotInst
} catch (e) {
throw e;
}
}

boot(config, handlers)
```

## ExpressJS server, webhooks

Two notes:
- You'll need to inject the server's publically-available host into config
- The webhook routes need to match in config's webhookUrl

```sh
npm install body-parser express speedybot webex-node-bot-framework
```

```ts
import { SpeedybotWebhook } from 'speedybot'
import { BotHandler, SpeedybotConfig } from 'speedybot'
import express from 'express'
import bodyParser from 'body-parser'

const app = express()
const port = process.env.PORT || 8000
app.use(bodyParser.json());
app.post('/ping', (req, res) => res.send('pong!'))

// handler list
const handlers: BotHandler[] = [
{
keyword: ['hello', 'hey', 'yo', 'watsup', 'hola'],
handler(bot, trigger) {
const reply = `Heya how's it going ${trigger.person.displayName}?`
bot.say(reply)
},
helpText: `A handler that greets the user`
}
]

// webhook
const config: SpeedybotConfig = {
token: `__REPLACE__ME__`,
webhookUrl: `https://123-45-678-910-987.ngrok.io/speedybotwebhook`
}

app.post('/speedybotwebhook', SpeedybotWebhook(config, handlers))

app.listen(port, () => {
console.log(`Listening + tunneled on port ${port}`)
})

```
If you need a tunnel, you can use **[speedyhelper](https://github.com/valgaze/speedyhelper#readme)** See here for **[important information](https://github.com/valgaze/speedyhelper/blob/master/docs/ngrok.md)**

```sh
npx speedyhelper 8000
```

<details><summary>yarn/npm</summary>

## npm

```
npm i speedyhelper -g
speedyhelper tunnel 8000
```

## yarn

```sh
yarn global all speedyhelper
speedyhelper tunnel 8000
```

</details>

<details><summary>plain javascript</summary>

## Simple

```
npm install speedybot webex-node-bot-framework
```

```js
const { Launch } = require('speedybot')

async function boot(config, handlers) {
try {
const speedybotInst = await Launch(config, handlers)
const phrase = config.webhookUrl ? `Webhook available here: ${config.webhookUrl}` : 'Connected using websockets'
console.log(`Success! ${phrase}`)
return speedybotInst
} catch (e) {
throw e;
}
}

const handlers = [
{
keyword: ['ping', 'pong'],
handler(bot, trigger) {
const normalized = trigger.text.toLowerCase()
if (normalized === 'ping') {
bot.say('pong')
} else {
bot.say('ping')
}
},
helpText: `A handler that says ping when the user says pong and vice versa`
}
]
const config = {
token: '__REPLACE__ME__',
}
boot(config, handlers)
```

```sh
npm install body-parser express speedybot webex-node-bot-framework
```

```js
/**
* $ npm install body-parser express speedybot webex-node-bot-framework
*
*/
const { SpeedybotWebhook } = require('speedybot')
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = process.env.PORT || 8000
app.use(bodyParser.json());
app.post('/ping', (req, res) => res.send('pong!'))


const handlers = [
{
keyword: ['hello', 'hey', 'yo', 'watsup', 'hola'],
handler(bot, trigger) {
const reply = `Heya how's it going ${trigger.person.displayName}?`
bot.say(reply)
},
helpText: `A handler that greets the user`
}
]

// webhook
const config = {
token: `__REPLACE__ME__`,
webhookUrl: `https://123-45-678-910-987.ngrok.io/speedybotwebhook`
}

app.post('/speedybotwebhook', SpeedybotWebhook(config, handlers))

app.listen(port, () => {
console.log(`Listening + tunneled on port ${port}`)
})
```


</details>
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"watch": ["src/", "util", "settings/**/*"],
"ext": "ts, json",
"ignore": [""],
"exec": "npm run start"
"exec": "npm run build"
}
Loading

0 comments on commit 1374d66

Please sign in to comment.