Skip to content

Commit

Permalink
feat: add all other commands
Browse files Browse the repository at this point in the history
  • Loading branch information
roziscoding committed Sep 1, 2022
1 parent cfb2472 commit 2b51ec7
Show file tree
Hide file tree
Showing 21 changed files with 135 additions and 102 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
plugins: ['@typescript-eslint'],
rules: {
'no-use-before-define': 0,
'no-useless-constructor': 0
'no-useless-constructor': 0,
'no-unused-vars': 0
}
}
10 changes: 6 additions & 4 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConversationFlavor, conversations as grammyConversations } from '@grammyjs/conversations'
import { Bot, Context, session, SessionFlavor } from 'grammy'
import { cancel, setInfo, start } from './commands'
import * as commands from './commands'
import { AppConfig } from './config'
import * as conversations from './conversations'

Expand Down Expand Up @@ -30,14 +30,16 @@ export async function getBot(config: AppConfig) {
bot.use(grammyConversations())

/** Cancel Command */
bot.command(cancel.name, cancel.fn)
bot.command(commands.cancel.name, commands.cancel.fn)

/** Conversations */
bot.use(conversations.setInfo)

/** Regular commands */
bot.command(start.name, start.fn)
bot.command(setInfo.name, setInfo.fn)
for (const command of Object.values(commands)) {
if (command.name === 'cancel') continue
bot.command(command.name, command.fn)
}

bot.catch(console.error)

Expand Down
3 changes: 2 additions & 1 deletion src/commands/cancel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AppContext } from '../bot'
import { Command } from '../domain/Command'

export const cancel = {
export const cancel: Command = {
name: 'cancel',
helpText: 'Cancela a operação atual',
fn: async (ctx: AppContext) => {
Expand Down
20 changes: 20 additions & 0 deletions src/commands/getInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { safeHtml, stripIndents } from 'common-tags'
import { format } from 'util'
import { Command } from '../domain/Command'

export const getInfo: Command = {
name: 'getinfo',
helpText: 'Exibe todas as informações que eu tenho sobre você',
fn: async (ctx) => {
const message = format(
'Aqui estão todas as informações que eu tenho sobre você:\n\n```\n%s```',
stripIndents(safeHtml)`
<b>Chave PIX</b>: ${ctx.session.pixKey}
<b>Cidade</b>: ${ctx.session.city}
<b>Nome</b>: ${ctx.session.name}
`
)

return ctx.reply(message, { parse_mode: 'HTML' })
}
}
18 changes: 18 additions & 0 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as commands from '.'
import { Command } from '../domain/Command'

const help: Command = {
name: 'help',
fn: async (ctx) => {
const message = [
'Aqui está a lista dos comandos mais importantes:\n',
...Object.values(commands)
.filter((command) => !!command.helpText)
.map((command) => `/${command.name}: ${command.helpText}`)
].join('\n')

return ctx.reply(message)
}
}

export default help
4 changes: 4 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export * from './cancel'
export * from './getInfo'
export * from './help'
export * from './privacy'
export * from './repo'
export * from './set-info'
export * from './start'
13 changes: 13 additions & 0 deletions src/commands/privacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { format } from 'util'
import { Command } from '../domain/Command'
import { PRIVACY_POLICY_URL, PRIVACY_TEXT } from '../util/strings'

export const privacy: Command = {
name: 'privacy',
helpText: 'Envia o link da política de privacidade do bot',
fn: async (ctx) => {
const message = format(PRIVACY_TEXT, PRIVACY_POLICY_URL)

return ctx.reply(message, { parse_mode: 'MarkdownV2' })
}
}
13 changes: 13 additions & 0 deletions src/commands/repo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { format } from 'util'
import { Command } from '../domain/Command'
import { REPO_URL } from '../util/strings'

export const repo: Command = {
name: 'repo',
helpText: 'Envia o link do repositório do bot',
fn: async (ctx) => {
const message = format('Para obter meu código fonte, acesse meu [repositório no GitHub](%s)', REPO_URL)

return ctx.reply(message, { parse_mode: 'MarkdownV2' })
}
}
6 changes: 3 additions & 3 deletions src/commands/set-info.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AppContext } from '../bot'
import { Command } from '../domain/Command'

export const setInfo = {
export const setInfo: Command = {
name: 'setinfo',
helpText: 'Define suas informações',
fn: async (ctx: AppContext) => {
fn: async (ctx) => {
return ctx.conversation.enter('setInfo')
}
}
7 changes: 3 additions & 4 deletions src/commands/start.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { InlineKeyboard } from 'grammy'
import { evaluateQuery } from '../../old/handleInlineQuery'
import { AppContext } from '../bot'
import { User } from '../domain/User'
import { AppContext, AppSession } from '../bot'
import { evaluateQuery } from '../util/query'

const KNOWN_MESSAGE = (user: User) =>
const KNOWN_MESSAGE = (user: AppSession) =>
`Opa, tudo certo? Eu já tenho seus dados do Pix aqui, olha só:
\`Chave: ${user.pixKey}\`
Expand Down
9 changes: 9 additions & 0 deletions src/commands/stop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Command } from '../domain/Command'

export const stop: Command = {
name: 'stop',
helpText: 'Apaga todos os dados que eu tenho armazenados sobre você',
fn: async (ctx) => {
return ctx.conversation.enter('stop')
}
}
2 changes: 1 addition & 1 deletion src/conversations/set-info.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Conversation, createConversation } from '@grammyjs/conversations'
import { oneLine, safeHtml, stripIndents } from 'common-tags'
import { InlineKeyboard } from 'grammy'
import { evaluateQuery } from '../../old/handleInlineQuery'
import { AppContext } from '../bot'
import { evaluateQuery } from '../util/query'

const PRIVACY_URL = 'https://github.com/roziscoding/amandapix-telegram-bot/blob/main/PRIVACY.md'

Expand Down
32 changes: 32 additions & 0 deletions src/conversations/stop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Conversation, createConversation } from '@grammyjs/conversations'
import { InlineKeyboard } from 'grammy'
import { AppContext } from '../bot'

const stop = async (converstion: Conversation<AppContext>, ctx: AppContext) => {
await ctx.reply('Deseja realmente apagar todos os seus dados?', {
reply_markup: new InlineKeyboard().text('Sim', 'y').text('Não', 'n')
})

const [confirmation, newContext] = await converstion
.waitFor('callback_query:data')
.then(async (ctx) => {
await ctx.editMessageReplyMarkup({})
return ctx
})
.then((ctx) => {
return [ctx.callbackQuery.data === 'y', ctx] as const
})

if (!confirmation) {
return ctx.reply('Beleza, não vou apagar nada então.')
}

newContext.session.name = ''
newContext.session.pixKey = ''
newContext.session.city = ''
delete newContext.session.query

return ctx.reply('Pronto. Excluí todos os dados que eu tinha sobre você')
}

export default createConversation(stop)
4 changes: 0 additions & 4 deletions src/domain/Code.ts

This file was deleted.

24 changes: 2 additions & 22 deletions src/domain/Command.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
import { Message, Params, Telegram } from 'typegram'
import { UserRepository } from '../repositories/users'
import { Markup, sendMessage } from '../util/telegram/sendMessage'
import { Awaitable } from '../util/types/Awaitable'
import { Response } from './Response'
import { User } from './User'

export type Context = {
user: User
repository: UserRepository
message: Message.TextMessage
match: RegExpMatchArray | null
command: Command
sendMessage: (
text: string,
markdown?: boolean,
markup?: Markup,
extra?: Partial<Params<'sendMessage', any>[0]>
) => ReturnType<typeof sendMessage>
}
import { AppContext } from '../bot'

export type Command = {
name: string
regex: RegExp
helpText?: string
fn: (ctx: Context) => Awaitable<Response<keyof Telegram> | null>
fn: (ctx: AppContext) => any
}
5 changes: 0 additions & 5 deletions src/domain/Response.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/domain/User.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/util/allowCors.ts

This file was deleted.

28 changes: 0 additions & 28 deletions src/util/makeRequest.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/util/pixCode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { pix } from 'pix-me'
import { User } from '../../old/domain/User'
import { AppSession } from '../bot'

export function getPixCodeForUser(user: User, value: string | number) {
export function getPixCodeForUser(user: AppSession, value: string | number) {
return pix({
key: user.pixKey,
amount: typeof value === 'number' ? value.toFixed(2) : value,
Expand Down
5 changes: 5 additions & 0 deletions src/util/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as math from 'mathjs'

export async function evaluateQuery(query: string): Promise<number> {
return math.round(math.evaluate(query), 2)
}

0 comments on commit 2b51ec7

Please sign in to comment.