Skip to content

Commit

Permalink
Fix currency scheduler lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneDraitsev committed Jan 2, 2025
1 parent 52ebd5f commit a8699a1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 26 deletions.
17 changes: 11 additions & 6 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ functions:
- http:
path: /
method: post

currency-scheduler:
handler: src/telegram-bot/src/currency-scheduler.default
environment:
TOKEN: ${env:TOKEN}
FIXER_API_KEY: ${env:FIXER_API_KEY}
EXCHANGE_RATE_API_KEY: ${env:EXCHANGE_RATE_API_KEY}
COIN_MARKET_CAP_API_KEY: ${env:COIN_MARKET_CAP_API_KEY}
CRYPTO_REQUESTS_BUCKET_NAME: ${self:custom.cryptoRequestsBucketName}
events:
- schedule:
name: kabold-chat-event
rate: cron(0 9,11,13,15,17 ? * MON-FRI *)
enabled: true
input:
message:
text: /c
chat:
id: -1001306676509.0

sharp-statistics:
handler: src/sharp-statistics/src/index.default
environment:
Expand Down
37 changes: 37 additions & 0 deletions src/telegram-bot/src/currency-scheduler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { type ParseModeFlavor, hydrateReply } from '@grammyjs/parse-mode'
import type { APIGatewayProxyHandler } from 'aws-lambda'
import { Bot, type Context } from 'grammy'

import { getCurrencyMessage } from './currency'

const bot = new Bot<ParseModeFlavor<Context>>(process.env.TOKEN || '', {
client: {
// We accept the drawback of webhook replies for typing status.
canUseWebhookReply: (method) => method === 'sendChatAction',
},
})

bot.use(hydrateReply)

const currencySchedulerHandler: APIGatewayProxyHandler = async () => {
try {
const CHAT_IDS = ['-1001306676509.0']

const message = await getCurrencyMessage()

for (const chatId of CHAT_IDS) {
await bot.api.sendMessage(chatId, message, { parse_mode: 'HTML' })
}

return { body: '', statusCode: 200 }
} catch (e) {
console.log(e)
return {
body: JSON.stringify({ message: 'Something went wrong' }),
// we need to send 200 here to avoid issue with telegram attempts to resend you a message
statusCode: 200,
}
}
}

export default currencySchedulerHandler
29 changes: 16 additions & 13 deletions src/telegram-bot/src/currency/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ const getError = (err: Error, from: string): string => {
return `Can't fetch currency from ${from}\n`
}

const setupCurrencyCommands = (bot: Bot<ParseModeFlavor<Context>>) => {
bot.command('c', async (ctx) => {
const promises = [
getMainCurrencies().catch((err) =>
getError(err, 'ExchangeRate and Fixer'),
),
getRussianCurrency().catch((err) => getError(err, 'meduza')),
getCryptoCurrency().catch((err) => getError(err, 'poloniex')),
]
export const getCurrencyMessage = async () => {
const promises = [
getMainCurrencies().catch((err) => getError(err, 'ExchangeRate and Fixer')),
getRussianCurrency().catch((err) => getError(err, 'meduza')),
getCryptoCurrency().catch((err) => getError(err, 'poloniex')),
]

const result = await Promise.all(promises).then(
(result) => `${result.join('\n')}`,
)

const result = await Promise.all(promises).then(
(result) => `${result.join('\n')}`,
)
return result
}

return ctx.replyWithHTML(result)
const setupCurrencyCommands = (bot: Bot<ParseModeFlavor<Context>>) => {
bot.command('c', async (ctx) => {
const message = await getCurrencyMessage()
return ctx.replyWithHTML(message)
})
}

Expand Down
9 changes: 2 additions & 7 deletions src/telegram-bot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,14 @@ setupExternalApisCommands(bot)
// /o <text> - generate chat completion with o1-mini
setupOpenAiCommands(bot)

export const telegramBotHandler: APIGatewayProxyHandler = async (
event,
context,
) => {
const telegramBotHandler: APIGatewayProxyHandler = async (event, context) => {
try {
const body = event.body ? JSON.parse(event.body) : event // Identify lambda call vs http event

await handleUpdate(
{ body: event.body ?? '', headers: event.headers },
context,
)

return { body: JSON.stringify({ body }), statusCode: 200 }
return { body: JSON.stringify({ body: event.body ?? '' }), statusCode: 200 }
} catch (e) {
console.log(e)
return {
Expand Down

0 comments on commit a8699a1

Please sign in to comment.