Skip to content

Commit

Permalink
fix authorizer, add long ttl and change schema name
Browse files Browse the repository at this point in the history
  • Loading branch information
jotacemarin committed Sep 8, 2024
1 parent baba12c commit dd1b401
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/functions/telegram_authorizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const extractToken = (authorizationToken: string) => {
const [_, token] = authorizationToken?.split(" ");
const cleanToken = atob(token);
const [clientId, clientSecret] = cleanToken?.split(":");
console.log("extractToken", `${clientId}, ${clientSecret}`);
return { clientId, clientSecret };
};

Expand Down
4 changes: 3 additions & 1 deletion src/functions/telegram_get_messages/function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ events:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/get-messages
method: GET
cors: true
authorizer: telegramAuthorizer
authorizer:
name: telegramAuthorizer
resultTtlInSeconds: 0
14 changes: 10 additions & 4 deletions src/functions/telegram_get_messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { APIGatewayEvent, Callback, Context } from "aws-lambda";
import { OK } from "http-status";
import { BAD_REQUEST, OK } from "http-status";
import { ChatMessageDao } from "../../lib/dao";

const execute = async (): Promise<any> => {
const execute = async (event: APIGatewayEvent): Promise<any> => {
if (!event?.queryStringParameters?.chat_id) {
return { statusCode: BAD_REQUEST };
}

const chatId = Number(event.queryStringParameters.chat_id);

await ChatMessageDao.initInstance();
const messages = await ChatMessageDao.getAll();
const messages = await ChatMessageDao.getAll(chatId);

return { statusCode: OK, body: JSON.stringify(messages) };
};
Expand All @@ -16,7 +22,7 @@ export const telegramGetMessages = async (
): Promise<void> => {
context.callbackWaitsForEmptyEventLoop = false;

const response = await execute();
const response = await execute(event);

return callback(null, response);
};
4 changes: 3 additions & 1 deletion src/functions/telegram_send_message/function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ events:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/send-message
method: POST
cors: true
authorizer: telegramAuthorizer
authorizer:
name: telegramAuthorizer
resultTtlInSeconds: 0
4 changes: 3 additions & 1 deletion src/functions/telegram_send_photo/function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ events:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/send-photo
method: POST
cors: true
authorizer: telegramAuthorizer
authorizer:
name: telegramAuthorizer
resultTtlInSeconds: 0
4 changes: 3 additions & 1 deletion src/functions/telegram_send_video/function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ events:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/send-video
method: POST
cors: true
authorizer: telegramAuthorizer
authorizer:
name: telegramAuthorizer
resultTtlInSeconds: 0
4 changes: 3 additions & 1 deletion src/functions/telegram_set_webhook/function.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ events:
path: /${self:custom.secrets.service.name}-${self:custom.secrets.provider.stage}/telegram/set-webhook
method: POST
cors: true
authorizer: telegramAuthorizer
authorizer:
name: telegramAuthorizer
resultTtlInSeconds: 0
10 changes: 6 additions & 4 deletions src/lib/dao/chatMessageDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ChatMessage } from "../models";
import { MongodbService } from "../services";

export class ChatMessageDao {
private static schemaName: string = "chatMessage";
private static schemaName: string = "chat_message";
private static chatMessageSchema: Schema<ChatMessage>;
public static chatMessageModel: Model<ChatMessage>;

Expand All @@ -15,7 +15,7 @@ export class ChatMessageDao {
if (!ChatMessageDao.chatMessageSchema) {
ChatMessageDao.chatMessageSchema = new Schema(
{
message: { type: Schema.Types.Mixed, required: true },
telegramMessage: { type: Schema.Types.Mixed, required: true },
expireAt: { type: Schema.Types.Date, expires: 43200 },
},
{
Expand All @@ -39,8 +39,10 @@ export class ChatMessageDao {
return ChatMessageDao.chatMessageModel.create(chatMessage);
}

public static async getAll(): Promise<Array<ChatMessage>> {
const chatMessages = await ChatMessageDao.chatMessageModel.find({}).exec();
public static async getAll(chatId: Number): Promise<Array<ChatMessage>> {
const chatMessages = await ChatMessageDao.chatMessageModel
.find({ telegramMessage: { message: { chat: { id: chatId } } } })
.exec();
if (!chatMessages || !chatMessages?.length) {
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/models/botnorrea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface Command {

export interface ChatMessage {
_id?: ID | string;
message: UpdateTg;
telegramMessage: UpdateTg;
expireAt?: Date | AtedAt | string;
createdAt?: AtedAt | string;
updatedAt?: AtedAt | string;
Expand Down

0 comments on commit dd1b401

Please sign in to comment.