Skip to content

Commit dfd18b9

Browse files
committed
test new version
1 parent 3414d65 commit dfd18b9

15 files changed

+230
-128
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ npm-debug.log*
2525
yarn-debug.log*
2626
yarn-error.log*
2727

28-
.vercel
28+
.vercel
29+
.env
30+
*.local

.prettierrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"singleQuote": false,
2+
"singleQuote": true,
33
"trailingComma": "all",
44
"arrowParens": "avoid",
55
"printWidth": 120,
66
"useTabs": true
7-
}
7+
}

api/index.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
import { NowRequest, NowResponse } from "@vercel/node";
1+
// // import { NowRequest, NowResponse } from "@vercel/node";
2+
// import { VercelRequest, VercelResponse } from "@vercel/node";
23

3-
import { useWebhook } from "../src/lib";
4+
// import { useWebhook } from "../src/lib";
45

5-
// main function
6-
export default async function handle(req: NowRequest, res: NowResponse) {
6+
// // main function
7+
// export default async function handle(req: VercelRequest, res: VercelResponse) {
8+
// try {
9+
// await useWebhook(req, res);
10+
// } catch (e) {
11+
// res.statusCode = 500;
12+
// res.json(e.message);
13+
// }
14+
// }
15+
16+
import { VercelRequest, VercelResponse } from "@vercel/node";
17+
import { startVercel } from "../src";
18+
19+
export default async function handle(req: VercelRequest, res: VercelResponse) {
720
try {
8-
await useWebhook(req, res);
9-
} catch (e) {
21+
await startVercel(req, res);
22+
} catch (e: any) {
1023
res.statusCode = 500;
11-
res.json(e.message);
12-
24+
res.setHeader("Content-Type", "text/html");
25+
res.end("<h1>Server Error</h1><p>Sorry, there was a problem</p>");
26+
console.error(e.message);
1327
}
1428
}

package.json

+33-26
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
{
2-
"name": "telechan-message-send-bot",
3-
"version": "2.0.0",
4-
"description": "功能简单的TG机器人",
5-
"main": "api/index.ts",
6-
"author": "EasyChen",
7-
"homepage": "https://twitter.com/easychen",
8-
"scripts": {
9-
"dev": "dotenv -- nodemon -e ts -x ts-node src/lib/telegram.ts",
10-
"prettier": "prettier --single-quote --trailing-comma all --write 'src/**/*.ts'"
11-
},
12-
"dependencies": {
13-
"axios": "^0.21.1",
14-
"md5": "^2.3.0",
15-
"telegraf": "^3.38.0"
16-
},
17-
"devDependencies": {
18-
"@sindresorhus/tsconfig": "^0.8.0",
19-
"@vercel/node": "^1.8.5",
20-
"debug": "^4.3.1",
21-
"dotenv-cli": "^4.0.0",
22-
"nodemon": "^2.0.6",
23-
"prettier": "^2.2.0",
24-
"ts-node": "^9.0.0",
25-
"typescript": "^4.1.2"
26-
}
27-
}
2+
"name": "telechan-message-send-bot-alterby-virola",
3+
"version": "2.0.1",
4+
"description": "功能简单的TG机器人",
5+
"main": "src/index.ts",
6+
"author": "EasyChen",
7+
"homepage": "https://twitter.com/easychen",
8+
"scripts": {
9+
"dev": "DEBUG=bot* dotenv -- nodemon -e ts -x ts-node src/index.ts",
10+
"devWindows": "@powershell -Command $env:DEBUG='bot*';dotenv -- nodemon -e ts -x ts-node src/index.ts",
11+
"build": "ncc build src/index.ts -o public -m",
12+
"prettier": "prettier --single-quote --trailing-comma all --write 'src/**/*.ts'"
13+
},
14+
"dependencies": {
15+
"axios": "^1.3.4",
16+
"md5": "^2.3.0",
17+
"telegraf": "^4.12.2"
18+
},
19+
"devDependencies": {
20+
"@sindresorhus/tsconfig": "^3.0.1",
21+
"@types/debug": "^4.1.7",
22+
"@types/md5": "^2.3.2",
23+
"@types/node": "^18.15.11",
24+
"@vercel/ncc": "^0.36.1",
25+
"@vercel/node": "^2.10.1",
26+
"debug": "^4.3.4",
27+
"dotenv-cli": "^7.1.0",
28+
"nodemon": "^2.0.22",
29+
"prettier": "^2.8.7",
30+
"telegraf": "^4.12.2",
31+
"ts-node": "^10.9.1",
32+
"typescript": "^5.0.2"
33+
}
34+
}

src/commands/about.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { author, homepage, name, version } from "../../package.json";
1+
import { author, homepage, name, version } from '../../package.json';
22

3-
const debug = require("debug")("bot:about_command");
3+
const debug = require('debug')('bot:about_command');
44

55
const about = () => (ctx: any) => {
66
const message = `*${name} ${version}*\n${author}\n${homepage}`;

src/commands/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from "./about";
2-
export * from "./sendkey";
1+
export * from './about';
2+
export * from './sendkey';

src/commands/sendkey.ts

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
import md5 from 'md5';
1+
const md5 = require('md5');
22
const VERCEL_URL = process.env.VERCEL_URL;
33
const TCKEY = process.env.TCKEY;
44

55
const replyToMessage = (ctx: any, messageId: string, string: string) =>
6-
ctx.reply(string, {
7-
reply_to_message_id: messageId,
8-
});
6+
ctx.reply(string, {
7+
reply_to_message_id: messageId,
8+
});
99

1010
const sendkey = () => (ctx: any) => {
11+
const messageId = ctx.message.message_id;
12+
const userName = ctx.from.last_name ? `${ctx.from.first_name} ${ctx.from.last_name}` : ctx.from.first_name;
13+
const sendkey = ctx.from.id + 'T' + md5(TCKEY + ctx.from.id);
1114

12-
const messageId = ctx.message.message_id;
13-
const userName = ctx.from.last_name ? `${ctx.from.first_name} ${ctx.from.last_name}` : ctx.from.first_name;
14-
const sendkey = ctx.from.id +'T'+md5(TCKEY+ctx.from.id);
15+
const site_url = String(VERCEL_URL).split('-')[0] + '.vercel.app';
1516

16-
const site_url = String(VERCEL_URL).split("-")[0]+'.vercel.app';
17-
18-
replyToMessage(ctx, messageId, `${userName} , Your sendkey is 🔑 ${sendkey} \n
17+
replyToMessage(
18+
ctx,
19+
messageId,
20+
`${userName} , Your sendkey is 🔑 ${sendkey} \n
1921
🚀 Use follow url to send message : \n
20-
${site_url}/api/send?sendkey=<sendkey>&text=<text>`);
21-
// replyToMessage(ctx, messageId, `Hello, ${userName} (user_id: ${ctx.from.id})! \n Your Message id is: ${messageId}`);
22+
${site_url}/api/send?sendkey=<sendkey>&text=<text>`,
23+
);
24+
// replyToMessage(ctx, messageId, `Hello, ${userName} (user_id: ${ctx.from.id})! \n Your Message id is: ${messageId}`);
2225
};
2326

2427
export { sendkey };

src/core/development.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Context, Telegraf } from 'telegraf';
2+
import { Update } from 'telegraf/typings/core/types/typegram';
3+
import createDebug from 'debug';
4+
5+
const debug = createDebug('bot:dev');
6+
7+
const development = async (bot: Telegraf<Context<Update>>) => {
8+
const botInfo = (await bot.telegram.getMe()).username;
9+
10+
debug('Bot runs in development mode');
11+
debug(`${botInfo} deleting webhook`);
12+
await bot.telegram.deleteWebhook();
13+
debug(`${botInfo} starting polling`);
14+
15+
await bot.launch();
16+
17+
process.once('SIGINT', () => bot.stop('SIGINT'));
18+
process.once('SIGTERM', () => bot.stop('SIGTERM'));
19+
};
20+
21+
export { development };

src/core/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './development';
2+
export * from './production';

src/core/production.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { VercelRequest, VercelResponse } from '@vercel/node';
2+
import createDebug from 'debug';
3+
import { Context, Telegraf } from 'telegraf';
4+
import { Update } from 'telegraf/typings/core/types/typegram';
5+
6+
const debug = createDebug('bot:dev');
7+
8+
const PORT = (process.env.PORT && parseInt(process.env.PORT, 10)) || 3000;
9+
const VERCEL_URL = `${process.env.VERCEL_URL}`;
10+
11+
const production = async (req: VercelRequest, res: VercelResponse, bot: Telegraf<Context<Update>>) => {
12+
debug('Bot runs in production mode');
13+
debug(`setting webhook: ${VERCEL_URL}`);
14+
15+
if (!VERCEL_URL) {
16+
throw new Error('VERCEL_URL is not set.');
17+
}
18+
19+
const getWebhookInfo = await bot.telegram.getWebhookInfo();
20+
if (getWebhookInfo.url !== VERCEL_URL + '/api') {
21+
debug(`deleting webhook ${VERCEL_URL}`);
22+
await bot.telegram.deleteWebhook();
23+
debug(`setting webhook: ${VERCEL_URL}/api`);
24+
await bot.telegram.setWebhook(`${VERCEL_URL}/api`);
25+
}
26+
27+
if (req.method === 'POST') {
28+
await bot.handleUpdate(req.body as unknown as Update, res);
29+
} else {
30+
res.status(200).json('Listening to bot events...');
31+
}
32+
debug(`starting webhook on port: ${PORT}`);
33+
};
34+
export { production };

src/index.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
1-
export * from "./commands";
2-
export * from "./text";
1+
import { Telegraf } from "telegraf";
2+
3+
import { about, sendkey } from "./commands";
4+
import { greeting } from "./text";
5+
import { VercelRequest, VercelResponse } from "@vercel/node";
6+
import { development, production } from "./core";
7+
8+
const BOT_TOKEN = process.env.BOT_TOKEN || "";
9+
const ENVIRONMENT = process.env.NODE_ENV || "";
10+
11+
const bot = new Telegraf(BOT_TOKEN);
12+
13+
bot.command("about", about()).command("sendkey", sendkey());
14+
bot.on("message", greeting());
15+
16+
//prod mode (Vercel)
17+
export const startVercel = async (req: VercelRequest, res: VercelResponse) => {
18+
await production(req, res, bot);
19+
};
20+
//dev mode
21+
ENVIRONMENT !== "production" && development(bot);

src/lib/responses.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {NowResponse} from '@vercel/node';
1+
import { VercelResponse } from '@vercel/node';
22

3-
export function badRequest(res: NowResponse, text = 'Bad Request') {
4-
res.status(400).json(text);
3+
export function badRequest(res: VercelResponse, text = 'Bad Request') {
4+
res.status(400).json(text);
55
}
66

7-
export function ok(res: NowResponse, text = 'Ok') {
8-
res.status(200).json(text);
7+
export function ok(res: VercelResponse, text = 'Ok') {
8+
res.status(200).json(text);
99
}

0 commit comments

Comments
 (0)