grammyjs / storages Goto Github PK
View Code? Open in Web Editor NEWStorage adapters for grammY sessions.
Storage adapters for grammY sessions.
Do I need to host my bot on Cloudflare to use Cloudflare adapter? Also, I saw that the example contain R2_BUCKET setting, can I use this instead of KV?
Currently all packages published on https://deno.land/x, which is impossible to update, because they moved from individual repos to monorepo.
Should i use https://nest.land then, maybe?
We should increase the time as the number of requests increases.
Originally posted by @Satont in #135 (comment)
Most likely, it's a good idea to double the time to wait with each attempt, capped at one hour.
After I managed to install this plugin, my bot completely stop working the way it ws before. Crash after crash. I`m not sure if I should not use it or can it be fixed is some way?
Error: 3 INVALID_ARGUMENT: Cannot convert an array value in an array value. at callErrorFromStatus
DEFAULT 2023-03-02T12:46:16.591903Z [resource.labels.functionName: valimisedBot] [labels.executionId: 1u84fc1geo2s] at /workspace/node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:225:29
Caused by: Error
DEFAULT 2023-03-02T12:46:16.591951Z [resource.labels.functionName: valimisedBot] [labels.executionId: 1u84fc1geo2s] at WriteBatch.commit (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:433:23)
DEFAULT 2023-03-02T12:46:16.591956Z [resource.labels.functionName: valimisedBot] [labels.executionId: 1u84fc1geo2s] at DocumentReference.set (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:392:27)
DEFAULT 2023-03-02T12:46:16.591961Z [resource.labels.functionName: valimisedBot] [labels.executionId: 1u84fc1geo2s] at Object.write (/workspace/node_modules/@grammyjs/storage-firestore/dist/cjs/index.js:11:39)
DEFAULT 2023-03-02T12:46:16.591968Z [resource.labels.functionName: valimisedBot] [labels.executionId: 1u84fc1geo2s] at PropertySession.finish (/workspace/node_modules/grammy/out/convenience/session.js:232:40)
DEBUG 2023-03-02T12:46:16.598473367Z [resource.labels.functionName: valimisedBot] [labels.executionId: 1u84fc1geo2s] Function execution took 1877 ms, finished with status: 'crash'
With this approach, we will be able to control the deno settings more precisely. This way we can exclude nodejs files from the deno language server.
Failed to load function definition from source: Failed to generate manifest from function source: Error: Cannot find module '/Users/xxx/vue/vite-project/node_modules/@grammyjs/storage-firestore/dist/cjs/mod.js'
The freeStorage seems to be down, giving a 503 error: 503 in free session service, retrying!
.
It would be good for users to know if the server is down. An easy way to do this, depending on the deployment, would be adding a server status badge to the README file.
Add eslint check before release and each commit
We should add base tsconfig
and extend it in children packages.
Found an ambigous SQL query builder for Deno and Node, need to check out what we can build on top of it.
https://deno.land/x/[email protected] can be used for it.
It is not very clear how to work with data from external storage to initialize the initial data of the user's session.
For example, when working with Redis adapter and lazySession
:
import { Bot, Context, LazySessionFlavor, Enhance, lazySession, enhanceStorage } from 'grammy'
import { RedisAdapter } from '@grammyjs/storage-redis'
import { Redis } from 'ioredis'
// ...
interface IUserSessionData {
counter: number
}
type TUserContext = Context & LazySessionFlavor<IUserSessionData>
const bot = new Bot<TUserContext>(process.env.TELEGRAM_BOT_TOKEN)
const redis = new Redis({
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASSWORD,
username: process.env.REDIS_USERNAME ? process.env.REDIS_USERNAME : 'default',
port: process.env.REDIS_PORT ? Number(process.env.REDIS_PORT) : 6379,
lazyConnect: true,
})
const enhanced = new RedisAdapter<Enhance<IUserSessionData>>({ instance: redis })
const storage = enhanceStorage({
storage: enhanced,
millisecondsToLive: 30 * 60 * 1000,
})
bot.use(
lazySession({
initial: () => ({ /* ??? */ }),
getSessionKey: (ctx: Omit<TUserContext, 'session'>) => ctx.from?.id.toString(),
storage,
}),
)
// ...
What do I need to do so that when creating middleware with session settings, I can pick up the current data? Now, every time the bot is rebooted, the session is reset to those specified in initial
block in lazySession
.
Since the session key in Redis is equal to the user ID in Telegram (according to the snippet above), in order to programmatically insert all data from Redis into the ctx.session
, I need to make a separate function, which should be at the beginning of each bot.*(...)
, etc.?
It would be convenient if the session initialization method could retrieve data from the remote storage itself.
And if there is no data (or the storage is unavailable), then take the specified fallback values.
Or let the developer configure the function himself, which will take data from Redis and put it in the ctx.session
of the current bot user:
bot.use(
lazySession({
initial: () => ({ counter: 0 }), // <-- fallback values here
getInitialSessionData: (ctx) => myFunc(ctx), // <-- send context with current Telegram user ID to get Redis record by key
getSessionKey: (ctx: Omit<TUserContext, 'session'>) => ctx.from?.id.toString(),
storage,
}),
)
I think, such an improvement would be useful for everyone (especially for beginners) and would make the bot code much easier.
Firestore cannot store conversations session data with current adapter.
Firestore cannot encode the function definitions of the stored update objects under log
e.g. copy, forward.
While the Admin SDK throws the error Error: Cannot encode value: (chat_id, other, signal)
, which wasn't too informative, testing with the Web SDK produced a more useful error Function setDoc() called with invalid data. Unsupported field value: a function
.
Perhaps the Firestore adapter should JSON.stringify / parse when writing to and reading from the database, at least for conversation sessions? It's slightly inelegant but can get the job done.
I could well be missing something very obvious, so would appreciate somebody suggesting a better approach. Thanks!
Using this import, like pointed out in the docs (https://grammy.dev/plugins/session#external-storage-solutions)
import { supabaseAdapter } from 'https://deno.land/x/grammy_storages/supabase/src/mod.ts';
throws this error, when trying to execute.
Warning Implicitly using latest version (v2.24.0) for https://deno.land/x/supabase/mod.ts
error: Module not found "https://deno.land/x/supabase/mod.ts".
at https://deno.land/x/[email protected]/supabase/src/deps.deno.ts:1:32
I suspect the supabase module import beeing deprecated, since it originates from an empty repo. I guess using https://esm.sh/@supabase/supabase-js@2 could do the trick.
Add support for nodejs into deno kv adapter
It's possible to add TTL check on read operations for all adapters.
Add prisma.io adapter, if possible.
I thought about it, and came to the conclusion that this should be taken care of by the user himself. I see a leak of abstraction here. The adapter is not doing just one thing that it should be doing.
The README needs to be updated after #202
Error in middleware: 3 INVALID_ARGUMENT: Cannot convert an array value in an array value.
Error: 3 INVALID_ARGUMENT: Cannot convert an array value in an array value.
at callErrorFromStatus (D:\Projects\code-now-bot\node_modules\@grpc\grpc-js\src\call.ts:81:17)
at Object.onReceiveStatus (D:\Projects\code-now-bot\node_modules\@grpc\grpc-js\src\client.ts:356:55)
at Object.onReceiveStatus (D:\Projects\code-now-bot\node_modules\@grpc\grpc-js\src\client-interceptors.ts:454:34)
at Object.onReceiveStatus (D:\Projects\code-now-bot\node_modules\@grpc\grpc-js\src\client-interceptors.ts:416:48)
at D:\Projects\code-now-bot\node_modules\@grpc\grpc-js\src\resolving-call.ts:111:24
at processTicksAndRejections (node:internal/process/task_queues:77:11)
export async function checkoutOrder(
conversation: MyConversation,
ctx: CodeNowBotContext,
) {
await ctx.reply(getTextByContext("questions.details.message", ctx));
const orderDetails = await conversation.form.text();
botLogger.debug(orderDetails);
return;
}
I got this when try to use conversations with firebase storage adapter
When writing a session, specially when using conversations, we don't really want to write undefined values as null to the database, because conversations expects some properties to be undefined, and when they're null, they break.
Reference: https://t.me/grammyjs/73326
If it makes sense, we could pass ignoreUndefined: true
to the updateOne
method, which would solve this problem.
Although, I don't know if it really makes sense, since we'll be changing the behavior of the adapter for inumerous cases exclusevely because of the conversations case, so maybe we should give the user an option to turn this on when installing the adapter, which would make them aware of this behavior, and could be enabled only when the user actually needs it to behave this way. What do you think?
I'm in the development stage and want to reset all the data associated with my bot token to start fresh. How can I clean up the free storage or check what it contains?
While trying to install the supabase storage adapter it fails with Could not resolve dependency: peer @supabase/supabase-js@"^1.0.0" from @grammyjs/[email protected]
There's no MIT license file in this repo, please add it
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
Warning
These dependencies are deprecated:
Datasource | Name | Replacement PR? |
---|---|---|
npm | @types/ioredis |
Warning
Renovate failed to look up the following dependencies: Failed to look up npm package @grammyjs/storage-utils
.
Files affected: packages/file/package.json
, packages/mongodb/package.json
, packages/prisma/package.json
, packages/psql/package.json
, packages/redis/package.json
, packages/typeorm/package.json
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.
packages/psql/docker-compose.yml
postgres 15
.github/workflows/deno_deps_update.yml
actions/checkout v3
denoland/setup-deno v1
peter-evans/create-pull-request v5
.github/workflows/pr_title_lint.yml
amannn/action-semantic-pull-request v5
.github/workflows/release_node.yml
actions/checkout v3
actions/setup-node v3
.github/workflows/tests.yml
actions/checkout v3
actions/setup-node v3
actions/checkout v3
maximousblk/setup-deno v2
actions/setup-node v3
libs/utils/package.json
grammy ^1.21.1
package.json
@types/node ^20.11.22
@typescript-eslint/eslint-plugin ^7.1.0
@typescript-eslint/parser ^7.1.0
deno2node ^1.11.0
eslint ^8.57.0
husky ^9.0.11
lerna ^8.1.2
lint-staged ^15.2.2
prettier ^3.2.5
rimraf ^5.0.5
tsx ^4.7.1
typescript ^5.3.3
pnpm >=7.0.0
node >=12.0.0
packages/cloudflare/package.json
@cloudflare/workers-types ^4.20240222.0
grammy ^1.21.1
wrangler ^3.30.0
packages/denodb/package.json
packages/denokv/package.json
packages/deta/package.json
node-fetch ^2.6.6
@types/node-fetch ^2.6.11
grammy ^1.21.1
packages/file/package.json
@grammyjs/storage-utils ^2.4.2
grammy ^1.21.1
packages/firestore/package.json
@google-cloud/firestore ^6.6.0
packages/free/package.json
node-fetch ^2.6.6
@types/node-fetch ^2.6.11
packages/mongodb/package.json
@grammyjs/storage-utils ^2.4.2
grammy ^1.21.1
mongodb ^6.3.0
mongodb-memory-server ^9.1.6
packages/pocketbase/package.json
pocketbase ^0.21.1
grammy ^1.21.1
packages/prisma/package.json
@grammyjs/storage-utils ^2.4.2
@prisma/client ^5.10.2
grammy ^1.21.1
prisma ^5.10.2
vite ^5.1.4
vitest ^1.3.1
packages/psql/package.json
@grammyjs/storage-utils ^2.4.2
@types/pg ^8.11.2
grammy ^1.21.1
pg ^8.11.3
pg-mem ^2.8.1
packages/redis/package.json
@grammyjs/storage-utils ^2.4.2
@types/ioredis ^5.0.0
grammy ^1.21.1
ioredis ^5.3.2
packages/supabase/package.json
@supabase/supabase-js ^1.24.0
@supabase/supabase-js ^1.0.0 || ^2.0.0
packages/typeorm/package.json
@grammyjs/storage-utils ^2.4.2
better-sqlite3 ^9.4.3
grammy ^1.21.1
mongodb ^6.3.0
mongodb-memory-server ^9.1.6
typeorm ^0.3.20
vite ^5.1.4
vitest ^1.3.1
package.json
"dependencies": {
"@grammyjs/files": "^1.0.3",
"@grammyjs/menu": "^1.0.4",
"@grammyjs/router": "^1.1.1",
"@satont/grammy-file-storage": "^1.2.0",
"filesize": "^8.0.7",
"grammy": "^1.7.1",
"https-proxy-agent": "^5.0.0",
"node-fetch": "^3.2.3",
"node-json-db": "^1.5.0",
"nodejs-file-downloader": "^4.9.3",
"sequelize": "^6.18.0",
"sqlite3": "^5.0.2"
}
use example code, get error
TSError: ⨯ Unable to compile TypeScript:
test.ts:24:7 - error TS2322: Type '() => { counter: number; }' is not assignable to type '() => undefined'.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.