Skip to content

Commit

Permalink
Decouple handler from processor
Browse files Browse the repository at this point in the history
  • Loading branch information
guerrerocarlos committed Nov 21, 2021
1 parent 703030f commit 0d0f506
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 35 deletions.
14 changes: 10 additions & 4 deletions serverless.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { AWS } from "@serverless/typescript";
import * as dotenv from "dotenv";
import telegram from "@functions/telegram";
import main from "@functions/main";

dotenv.config();

Expand All @@ -11,9 +12,9 @@ const serverlessConfiguration: AWS = {
webpack: {
webpackConfig: "./webpack.config.js",
includeModules: {
forceExclude: ['aws-sdk']
forceExclude: ["aws-sdk"],
},
}
},
},
plugins: ["serverless-webpack"],
provider: {
Expand All @@ -32,7 +33,7 @@ const serverlessConfiguration: AWS = {
TEMP_STORAGE_FOLDER: process.env.TEMP_STORAGE_FOLDER,
WIT_TOKEN: process.env.WIT_TOKEN,
GC_BUCKET: process.env.GC_BUCKET,
GC_CREDENTIALS: process.env.GC_CREDENTIALS
GC_CREDENTIALS: process.env.GC_CREDENTIALS,
},
deploymentBucket: {
name: "deployment-bucket-us-east-2",
Expand All @@ -41,6 +42,11 @@ const serverlessConfiguration: AWS = {
iam: {
role: {
statements: [
{
Effect: "Allow",
Action: ["lambda:InvokeFunction"],
Resource: ["*"],
},
{
Effect: "Allow",
Action: ["s3:ListBucket"],
Expand Down Expand Up @@ -70,7 +76,7 @@ const serverlessConfiguration: AWS = {
},
},
// import the function via paths
functions: { telegram }
functions: { telegram, main },
};

module.exports = serverlessConfiguration;
2 changes: 1 addition & 1 deletion setWebhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ const bot = new Telegraf(process.env.BOT_TOKEN, {
});

bot.telegram.setWebhook(
"https://ilpifex4si.execute-api.us-east-2.amazonaws.com/dev/telegram"
"https://ilpifex4si.execute-api.us-east-2.amazonaws.com/dev/main"
);
1 change: 1 addition & 0 deletions src/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as telegram } from './telegram';
export { default as main } from './main';
19 changes: 19 additions & 0 deletions src/functions/main/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import "source-map-support/register";

import { invokeFunction } from "../../utils/lambda";

export const main = (event, context, cb) => {
console.log("🔥🔥", JSON.stringify(event, null, 2));

invokeFunction("voicebot-dev-telegram", { event, context });

const response = {
statusCode: 200,
body: "OK",
};
cb(null, response);
};

// bot.telegram.setWebhook(
// "https://ilpifex4si.execute-api.us-east-2.amazonaws.com/dev/telegram"
// );
14 changes: 14 additions & 0 deletions src/functions/main/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import schema from './schema';
import { handlerPath } from '@libs/handlerResolver';

export default {
handler: `${handlerPath(__dirname)}/handler.main`,
events: [
{
http: {
method: 'any',
path: 'main',
}
}
]
}
43 changes: 13 additions & 30 deletions src/functions/telegram/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,19 @@ bot.on("voice", async function (ctx) {
}
}, 3000);

const timeout = new Promise((_resolve, _reject) => {
setTimeout(_resolve, 20000, "Could not transcribe, too long :(");
});

console.log("replying...");

let result = Promise.race([
await googleTranscribe(fileLink),
timeout,
]) as any;

let result = await googleTranscribe(fileLink) as any
clearInterval(interval);

console.log("result", JSON.stringify(result, null, 2));

if (result.transcription) {
ctx.telegram.editMessageText(
ctx.chat.id,
translatingMessage.message_id,
undefined,
result.transcription
);
} else {
ctx.telegram.editMessageText(
ctx.chat.id,
translatingMessage.message_id,
undefined,
"Could not transcribe, invalid duration."
);
}
console.log("googleTranscribeResults", result)

await ctx.telegram.editMessageText(
ctx.chat.id,
translatingMessage.message_id,
undefined,
result.transcription
);
} catch (err) {
await ctx.telegram.editMessageText(
ctx.chat.id,
Expand All @@ -111,11 +94,11 @@ bot.on("voice", async function (ctx) {
}
});

export const main = (event, cb) => {
console.log("🔥", JSON.stringify(event, null, 2));
return makeHandler(bot.webhookCallback("/telegram"))(event, cb);
export const main = (evt, cb) => {
console.log("🔥", JSON.stringify(evt.event, null, 2));
return makeHandler(bot.webhookCallback("/main"))(evt.event, evt.context);
};

// bot.telegram.setWebhook(
// "https://ilpifex4si.execute-api.us-east-2.amazonaws.com/dev/telegram"
// "https://ilpifex4si.execute-api.us-east-2.amazonaws.com/dev/async"
// );
19 changes: 19 additions & 0 deletions src/utils/lambda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as AWS from "aws-sdk";

export function invokeFunction(FunctionName, Payload) {
var lambda = new AWS.Lambda({ apiVersion: "2015-03-31" });
return lambda.invoke(
{
FunctionName,
Payload: JSON.stringify(Payload),
InvocationType: "Event",
},
function (err, data) {
if (err) console.log(err, err.stack);
// an error occurred
else console.log(data); // successful response
}
);

// request.send();
}

0 comments on commit 0d0f506

Please sign in to comment.