Skip to content

Commit

Permalink
added button for when credits run out
Browse files Browse the repository at this point in the history
  • Loading branch information
theofandrich committed Sep 30, 2023
1 parent f5bcc40 commit 8618ef9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/modules/open-ai/api/openAi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ export const streamChatCompletion = async (
}
completion = completion.replaceAll('...', '')
completion += '...'
// console.log(wordCount);
wordCount = 0
await ctx.api
.editMessageText(ctx.chat?.id, msgId, completion)
Expand Down
14 changes: 12 additions & 2 deletions src/modules/open-ai/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GrammyError } from 'grammy'
import { GrammyError, InlineKeyboard } from 'grammy'
import OpenAI from 'openai'
import { type Logger, pino } from 'pino'

Expand Down Expand Up @@ -43,6 +43,7 @@ import {
import { getCrawlerPrice, getWebContent } from './utils/web-crawler'
import * as Sentry from '@sentry/node'
import { now } from '../../utils/perf'
import { Callbacks } from '../types'

export class OpenAIBot implements PayableBot {
public readonly module = 'OpenAIBot'
Expand Down Expand Up @@ -782,11 +783,20 @@ export class OpenAIBot implements PayableBot {
const { totalCreditsAmount } = await chatService.getUserCredits(accountId)
const balance = addressBalance.plus(totalCreditsAmount)
const balanceOne = this.payments.toONE(balance, false).toFixed(2)

const buyCreditsButton = new InlineKeyboard().text(
'Buy now',
Callbacks.CreditsFiatBuy
)

const balanceMessage = appText.notEnoughBalance
.replaceAll('$CREDITS', balanceOne)
.replaceAll('$WALLET_ADDRESS', account?.address ?? '')
ctx.transient.analytics.sessionState = RequestState.Error
await sendMessage(ctx, balanceMessage, { parseMode: 'Markdown' }).catch(async (e) => { await this.onError(ctx, e) })
await sendMessage(ctx, balanceMessage, {
parseMode: 'Markdown',
reply_markup: buyCreditsButton
}).catch(async (e) => { await this.onError(ctx, e) })
ctx.transient.analytics.actualResponseTime = now()
}

Expand Down
9 changes: 8 additions & 1 deletion src/modules/payment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,18 @@ export class BotPayments {
const { totalCreditsAmount } = await chatService.getUserCredits(accountId)
const totalBalance = oneBalance.plus(totalCreditsAmount)
const creditsFormatted = this.toONE(totalBalance, false).toFixed(2)

const buyCreditsButton = new InlineKeyboard().text(
'Buy now',
Callbacks.CreditsFiatBuy
)

await sendMessage(ctx,
`Your credits: ${creditsFormatted} ONE tokens. To recharge, send to \`${userAccount.address}\`.`,
{
parseMode: 'Markdown',
replyId: message_id
replyId: message_id,
reply_markup: buyCreditsButton
}
)
return false
Expand Down

0 comments on commit 8618ef9

Please sign in to comment.