Skip to content

Commit 851301f

Browse files
committed
Initial commit
0 parents  commit 851301f

File tree

16 files changed

+1140
-0
lines changed

16 files changed

+1140
-0
lines changed

.github/workflows/deno.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
6+
# This workflow will install Deno and run tests across stable and nightly builds on Windows, Ubuntu and macOS.
7+
# For more information see: https://github.com/denolib/setup-deno
8+
9+
name: grammY stream
10+
11+
on:
12+
push:
13+
branches: [main]
14+
pull_request:
15+
branches: [main]
16+
17+
jobs:
18+
backport:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Setup repo
22+
uses: actions/checkout@v4
23+
24+
- name: Install dependencies
25+
run: npm install --ignore-scripts
26+
27+
- name: Run backporting
28+
run: npm run backport
29+
30+
format-and-lint:
31+
runs-on: ubuntu-latest
32+
steps:
33+
- name: Setup repo
34+
uses: actions/checkout@v4
35+
36+
- uses: denoland/setup-deno@v2
37+
with:
38+
deno-version: v2.x
39+
40+
- name: Check Format
41+
run: deno fmt --check
42+
43+
- name: Lint
44+
run: deno lint
45+
46+
test:
47+
runs-on: ${{ matrix.os }} # runs a test on Ubuntu, Windows and macOS
48+
49+
strategy:
50+
matrix:
51+
os: [macOS-latest, windows-latest, ubuntu-latest]
52+
53+
steps:
54+
- name: Setup repo
55+
uses: actions/checkout@v4
56+
57+
- uses: denoland/setup-deno@v2
58+
with:
59+
deno-version: v2.x
60+
61+
- name: Cache Dependencies
62+
run: deno task check
63+
64+
- name: Run Tests
65+
run: deno task test

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Dependency directories
15+
node_modules/
16+
17+
# package-lock.json will not be published, so no need to store it
18+
package-lock.json
19+
20+
# Output of 'npm pack'
21+
*.tgz
22+
23+
# Test coverage
24+
test/coverage
25+
test/cov_profile
26+
coverage.lcov
27+
28+
# Build output
29+
out/
30+
docs/

.vscode/extensions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["denoland.vscode-deno"]
3+
}

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.codeActionsOnSave": {
4+
"source.fixAll": "explicit"
5+
}
6+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 KnorpelSenf
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# grammY stream
2+
3+
Stream long text messages to Telegram.
4+
Make LLM output appear as animated message drafts before sending the message.
5+
Automatically split long text across several messages.
6+
7+
## Quickstart
8+
9+
Run `npm i grammy @grammyjs/stream @grammyjs/auto-retry` and paste the following code:
10+
11+
```ts
12+
import { Bot, type Context } from "grammy";
13+
import { autoRetry } from "@grammyjs/auto-retry";
14+
import { stream, type StreamFlavor } from "@grammyjs/stream";
15+
16+
type MyContext = StreamFlavor<Context>;
17+
18+
const bot = new Bot<MyContext>("");
19+
20+
bot.api.config.use(autoRetry());
21+
bot.use(stream());
22+
23+
async function* slowText() {
24+
// emulate slow text generation
25+
yield "This i";
26+
await new Promise((r) => setTimeout(r, 1000));
27+
yield "s some sl";
28+
await new Promise((r) => setTimeout(r, 1000));
29+
yield "owly gene";
30+
await new Promise((r) => setTimeout(r, 1000));
31+
yield "rated text";
32+
}
33+
34+
bot.command("stream", async (ctx) => {
35+
await ctx.replyWithStream(slowText());
36+
});
37+
38+
bot.command("start", (ctx) => ctx.reply("Hi! Send /stream"));
39+
bot.use((ctx) => ctx.reply("What a nice update."));
40+
41+
bot.start();
42+
```
43+
44+
For example, if you use the [AI SDK](https://ai-sdk.dev), your AI setup could look like this:
45+
46+
```ts
47+
import { streamText } from "ai";
48+
import { google } from "npm:@ai-sdk/google";
49+
50+
bot.command("credits", async (ctx) => {
51+
// Send prompt to LLM:
52+
const { textStream } = streamText({
53+
model: google("gemini-2.5-flash"),
54+
prompt: "How cool are grammY bots?",
55+
});
56+
57+
// Automatically stream response with grammY:
58+
await ctx.replyWithStream(textStream);
59+
});
60+
```

deno.jsonc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"nodeModulesDir": "none",
3+
"lock": false,
4+
"tasks": {
5+
"check": "deno cache --allow-import --check=all src/mod.ts",
6+
"build": "deno run --no-prompt --allow-import --allow-read=. --allow-write=. https://deno.land/x/deno2node@v1.16.0/src/cli.ts tsconfig.json",
7+
"test": "deno test --allow-import test",
8+
"ok": "deno fmt && deno lint && deno task test && deno task check",
9+
"clean": "git clean -fX out"
10+
},
11+
"exclude": [
12+
"./node_modules/",
13+
"./out/",
14+
"./package-lock.json",
15+
"./test/cov_profile"
16+
],
17+
"fmt": {
18+
"indentWidth": 4,
19+
"proseWrap": "preserve"
20+
}
21+
}

package.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "@grammyjs/stream",
3+
"description": "Stream long text messages to Telegram",
4+
"version": "0.0.1",
5+
"author": "KnorpelSenf",
6+
"license": "MIT",
7+
"engines": {
8+
"node": "^12.20.0 || >=14.13.1"
9+
},
10+
"homepage": "https://grammy.dev/plugins/stream",
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/grammyjs/grammY"
14+
},
15+
"scripts": {
16+
"prepare": "npm run backport",
17+
"backport": "deno2node tsconfig.json"
18+
},
19+
"peerDependencies": {
20+
"grammy": "^1.39.0"
21+
},
22+
"devDependencies": {
23+
"@types/node": "^12.20.55",
24+
"deno2node": "^1.16.0"
25+
},
26+
"files": [
27+
"out/"
28+
],
29+
"main": "./out/mod.js",
30+
"types": "./out/mod.d.ts",
31+
"keywords": [
32+
"telegram",
33+
"bot",
34+
"api",
35+
"client",
36+
"framework",
37+
"library",
38+
"grammy",
39+
"stream",
40+
"draft",
41+
"ai",
42+
"llm",
43+
"model"
44+
]
45+
}

src/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# grammY stream
2+
3+
This plugin simplifies streaming text (including LLM output) to Telegram.
4+
5+
Check out the [main page of the repository](https://github.com/grammyjs/stream).

src/deps.deno.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export {
2+
type Context,
3+
type MiddlewareFn,
4+
type RawApi,
5+
} from "https://lib.deno.dev/x/grammy@v1/mod.ts";
6+
export type {
7+
ApiMethods,
8+
Message,
9+
MessageEntity,
10+
} from "https://lib.deno.dev/x/grammy@v1/types.ts";

0 commit comments

Comments
 (0)