From 8618ef9fcb8dd82ccc7c7a6343196dd547f38767 Mon Sep 17 00:00:00 2001 From: Theo Fandrich Date: Sat, 30 Sep 2023 14:06:08 -0700 Subject: [PATCH] added button for when credits run out --- src/modules/open-ai/api/openAi.ts | 1 - src/modules/open-ai/index.ts | 14 ++++++++++++-- src/modules/payment/index.ts | 9 ++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/modules/open-ai/api/openAi.ts b/src/modules/open-ai/api/openAi.ts index 49d2abd8..dc7b8188 100644 --- a/src/modules/open-ai/api/openAi.ts +++ b/src/modules/open-ai/api/openAi.ts @@ -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) diff --git a/src/modules/open-ai/index.ts b/src/modules/open-ai/index.ts index 2d76cc33..a37827b0 100644 --- a/src/modules/open-ai/index.ts +++ b/src/modules/open-ai/index.ts @@ -1,4 +1,4 @@ -import { GrammyError } from 'grammy' +import { GrammyError, InlineKeyboard } from 'grammy' import OpenAI from 'openai' import { type Logger, pino } from 'pino' @@ -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' @@ -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() } diff --git a/src/modules/payment/index.ts b/src/modules/payment/index.ts index 2c17f348..630c0447 100644 --- a/src/modules/payment/index.ts +++ b/src/modules/payment/index.ts @@ -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