Skip to content

Instantly share code, notes, and snippets.

@adelowo
Last active January 20, 2020 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adelowo/86e4e0b63ed07945760978de4b2de208 to your computer and use it in GitHub Desktop.
Save adelowo/86e4e0b63ed07945760978de4b2de208 to your computer and use it in GitHub Desktop.
'use strict';
const serverless = require('serverless-http');
const express = require('express');
const bodyParser = require('body-parser');
const StreamChat = require('stream-chat').StreamChat;
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const client = new StreamChat(process.env.API_KEY, process.env.API_SECRET);
const channel = client.channel('messaging', 'lambda-webhook-chat');
channel.create();
app.post('/users/create', (req, res) => {
const username = req.body.username;
if (username === undefined || username.length == 0) {
res.status(400).send({
status: false,
message: 'Please provide your username',
});
return;
}
res.status(200);
res.send({
status: true,
message: 'You have been successfully authenticated',
token: client.createToken(username),
user_id: username,
});
});
app.post('/users/add_member', (req, res) => {
const username = req.body.username;
if (username === undefined || username.length == 0) {
res.status(400).send({
status: false,
message: 'Please provide your username',
});
return;
}
channel
.addMembers([username])
.then(() => {
res.status(200).send({ status: true });
})
.catch(err => {
console.log(err);
res.status(200).send({ status: false });
});
});
module.exports.hello = serverless(app);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment