Skip to content

Instantly share code, notes, and snippets.

@LukasForst
Last active May 23, 2021 16:48
Show Gist options
  • Save LukasForst/6e8bfb4170f679aa3c1ec0906580f20a to your computer and use it in GitHub Desktop.
Save LukasForst/6e8bfb4170f679aa3c1ec0906580f20a to your computer and use it in GitHub Desktop.
Simple Wire Bot
/**
* This is a very simple bot, that echos text it receives.
* For a sake of simplicity, we don't don any checks and assume only happy scenarios.
*
* We used Deno with Oak, to have a single executable file as short as possible without
* any boiler plate.
*
* Run as "deno run --allow-net echo.ts".
*/
import { Application, Context } from 'https://deno.land/x/oak/mod.ts';
// create an Oak Application
const app = new Application();
// register middleware (in our case it's an endpoint)
app.use(async (ctx: Context) => {
// assume that we're getting a POST/PUT request with JSON payload, for all possible payloads see
// https://github.com/wireapp/roman#events-that-are-sent-as-http-post-to-your-endpoint-webhook-or-websocket
const body = await ctx.request.body({ type: 'json' }).value;
// only respond to text events
if (body.type === 'conversation.new_text') {
// echo text back to the conversation by responding to a REST call from Roman
ctx.response.body = { type: 'text', text: { data: `You said: ${body.text.data}` } };
}
// respond to all request with status code 200
ctx.response.status = 200;
});
// start the web server
await app.listen({ port: 8080 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment