mirror of
https://github.com/tiennm99/miti-telegram.git
synced 2026-04-17 09:20:55 +00:00
feat: cleanup
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
# http://editorconfig.org
|
||||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = tab
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[*.yml]
|
||||
indent_style = space
|
||||
167
.gitignore
vendored
167
.gitignore
vendored
@@ -1,167 +0,0 @@
|
||||
# Logs
|
||||
|
||||
logs
|
||||
_.log
|
||||
npm-debug.log_
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
.pnpm-debug.log*
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
|
||||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# Runtime data
|
||||
|
||||
pids
|
||||
_.pid
|
||||
_.seed
|
||||
\*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
|
||||
coverage
|
||||
\*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# Snowpack dependency directory (https://snowpack.dev/)
|
||||
|
||||
web_modules/
|
||||
|
||||
# TypeScript cache
|
||||
|
||||
\*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
|
||||
.eslintcache
|
||||
|
||||
# Optional stylelint cache
|
||||
|
||||
.stylelintcache
|
||||
|
||||
# Microbundle cache
|
||||
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
|
||||
\*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
|
||||
.yarn-integrity
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
|
||||
.cache
|
||||
.parcel-cache
|
||||
|
||||
# Next.js build output
|
||||
|
||||
.next
|
||||
out
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
|
||||
.cache/
|
||||
|
||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
|
||||
.vuepress/dist
|
||||
|
||||
# vuepress v2.x temp and cache directory
|
||||
|
||||
.temp
|
||||
.cache
|
||||
|
||||
# Docusaurus cache and generated files
|
||||
|
||||
.docusaurus
|
||||
|
||||
# Serverless directories
|
||||
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
|
||||
.tern-port
|
||||
|
||||
# Stores VSCode versions used for testing VSCode extensions
|
||||
|
||||
.vscode-test
|
||||
|
||||
# yarn v2
|
||||
|
||||
.yarn/cache
|
||||
.yarn/unplugged
|
||||
.yarn/build-state.yml
|
||||
.yarn/install-state.gz
|
||||
.pnp.\*
|
||||
|
||||
# wrangler project
|
||||
|
||||
.dev.vars*
|
||||
!.dev.vars.example
|
||||
.env*
|
||||
!.env.example
|
||||
.wrangler/
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"printWidth": 140,
|
||||
"singleQuote": true,
|
||||
"semi": true,
|
||||
"useTabs": true
|
||||
}
|
||||
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@@ -1,5 +0,0 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"wrangler.json": "jsonc"
|
||||
}
|
||||
}
|
||||
151
index.js
Normal file
151
index.js
Normal file
@@ -0,0 +1,151 @@
|
||||
export default {
|
||||
async fetch(request, env, ctx) {
|
||||
// Handle CORS preflight requests
|
||||
if (request.method === 'OPTIONS') {
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
'Access-Control-Max-Age': '86400',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (request.method !== 'POST') {
|
||||
return new Response('Method not allowed', {
|
||||
status: 405,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const contentType = request.headers.get('content-type');
|
||||
let text;
|
||||
|
||||
if (contentType && contentType.includes('application/json')) {
|
||||
const body = await request.json();
|
||||
text = body.text;
|
||||
} else if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
|
||||
const formData = await request.formData();
|
||||
text = formData.get('text');
|
||||
} else {
|
||||
return new Response('Content-Type must be application/json or application/x-www-form-urlencoded', {
|
||||
status: 400,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!text) {
|
||||
return new Response('Missing text parameter', {
|
||||
status: 400,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Collect client request information
|
||||
const clientIP = request.headers.get('CF-Connecting-IP') ||
|
||||
request.headers.get('X-Forwarded-For') ||
|
||||
request.headers.get('X-Real-IP') ||
|
||||
'Unknown';
|
||||
|
||||
const userAgent = request.headers.get('User-Agent') || 'Unknown';
|
||||
const country = request.cf?.country || 'Unknown';
|
||||
const city = request.cf?.city || 'Unknown';
|
||||
const region = request.cf?.region || 'Unknown';
|
||||
const latitude = request.cf?.latitude || 'Unknown';
|
||||
const longitude = request.cf?.longitude || 'Unknown';
|
||||
const timezone = request.cf?.timezone || 'Unknown';
|
||||
const url = request.url || 'Unknown';
|
||||
const timestamp = new Date().toISOString();
|
||||
|
||||
// Format the message with request information
|
||||
const hasCoordinates = latitude !== 'Unknown' && longitude !== 'Unknown';
|
||||
const mapLink = hasCoordinates
|
||||
? `https://www.google.com/maps?q=${latitude},${longitude}`
|
||||
: null;
|
||||
|
||||
const requestInfo = `<b>IP</b>: <code>${clientIP}</code> <a href="https://ipinfo.io/${clientIP}">🔍</a>
|
||||
<b>Browser</b>: <code>${userAgent}</code>
|
||||
<b>Country</b>: <code>${country}</code>
|
||||
<b>Region</b>: <code>${region}</code>
|
||||
<b>City</b>: <code>${city}</code>
|
||||
<b>Coordinates</b>: <code>${latitude}, ${longitude}</code>${mapLink ? ` <a href="${mapLink}">📍</a>` : ''}
|
||||
<b>Timezone</b>: <code>${timezone}</code>
|
||||
<b>Timestamp</b>: <code>${timestamp}</code>
|
||||
<b>Original text</b>:`;
|
||||
|
||||
const formattedMessage = `${requestInfo}
|
||||
|
||||
${text}`;
|
||||
|
||||
const telegramToken = env.TELEGRAM_TOKEN;
|
||||
const telegramChatId = env.TELEGRAM_CHAT_ID;
|
||||
|
||||
if (!telegramToken || !telegramChatId) {
|
||||
return new Response('Missing TELEGRAM_TOKEN or TELEGRAM_CHAT_ID environment variables', {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const telegramUrl = `https://api.telegram.org/bot${telegramToken}/sendMessage`;
|
||||
const telegramPayload = {
|
||||
chat_id: telegramChatId,
|
||||
text: formattedMessage,
|
||||
parse_mode: 'HTML'
|
||||
};
|
||||
|
||||
const telegramResponse = await fetch(telegramUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(telegramPayload)
|
||||
});
|
||||
|
||||
const telegramResult = await telegramResponse.json();
|
||||
|
||||
if (!telegramResponse.ok) {
|
||||
return new Response(JSON.stringify({
|
||||
success: false
|
||||
}), {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify({
|
||||
success: true
|
||||
}), {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
return new Response(JSON.stringify({
|
||||
success: false
|
||||
}), {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
3069
package-lock.json
generated
3069
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
17
package.json
17
package.json
@@ -1,16 +1,5 @@
|
||||
{
|
||||
"name": "miti-telegram",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"deploy": "wrangler deploy",
|
||||
"dev": "wrangler dev",
|
||||
"start": "wrangler dev",
|
||||
"test": "vitest"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cloudflare/vitest-pool-workers": "^0.8.71",
|
||||
"vitest": "~3.2.0",
|
||||
"wrangler": "^4.33.1"
|
||||
}
|
||||
"devDependencies": {
|
||||
"wrangler": "^4.47.0"
|
||||
}
|
||||
}
|
||||
|
||||
151
src/index.js
151
src/index.js
@@ -1,151 +0,0 @@
|
||||
export default {
|
||||
async fetch(request, env, ctx) {
|
||||
// Handle CORS preflight requests
|
||||
if (request.method === 'OPTIONS') {
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
'Access-Control-Max-Age': '86400',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (request.method !== 'POST') {
|
||||
return new Response('Method not allowed', {
|
||||
status: 405,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
const contentType = request.headers.get('content-type');
|
||||
let text;
|
||||
|
||||
if (contentType && contentType.includes('application/json')) {
|
||||
const body = await request.json();
|
||||
text = body.text;
|
||||
} else if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
|
||||
const formData = await request.formData();
|
||||
text = formData.get('text');
|
||||
} else {
|
||||
return new Response('Content-Type must be application/json or application/x-www-form-urlencoded', {
|
||||
status: 400,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!text) {
|
||||
return new Response('Missing text parameter', {
|
||||
status: 400,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Collect client request information
|
||||
const clientIP = request.headers.get('CF-Connecting-IP') ||
|
||||
request.headers.get('X-Forwarded-For') ||
|
||||
request.headers.get('X-Real-IP') ||
|
||||
'Unknown';
|
||||
|
||||
const userAgent = request.headers.get('User-Agent') || 'Unknown';
|
||||
const country = request.cf?.country || 'Unknown';
|
||||
const city = request.cf?.city || 'Unknown';
|
||||
const region = request.cf?.region || 'Unknown';
|
||||
const latitude = request.cf?.latitude || 'Unknown';
|
||||
const longitude = request.cf?.longitude || 'Unknown';
|
||||
const timezone = request.cf?.timezone || 'Unknown';
|
||||
const url = request.url || 'Unknown';
|
||||
const timestamp = new Date().toISOString();
|
||||
|
||||
// Format the message with request information
|
||||
const hasCoordinates = latitude !== 'Unknown' && longitude !== 'Unknown';
|
||||
const mapLink = hasCoordinates
|
||||
? `https://www.google.com/maps?q=${latitude},${longitude}`
|
||||
: null;
|
||||
|
||||
const requestInfo = `<b>IP</b>: <code>${clientIP}</code> <a href="https://ipinfo.io/${clientIP}">🔍</a>
|
||||
<b>Browser</b>: <code>${userAgent}</code>
|
||||
<b>Country</b>: <code>${country}</code>
|
||||
<b>Region</b>: <code>${region}</code>
|
||||
<b>City</b>: <code>${city}</code>
|
||||
<b>Coordinates</b>: <code>${latitude}, ${longitude}</code>${mapLink ? ` <a href="${mapLink}">📍</a>` : ''}
|
||||
<b>Timezone</b>: <code>${timezone}</code>
|
||||
<b>Timestamp</b>: <code>${timestamp}</code>
|
||||
<b>Original text</b>:`;
|
||||
|
||||
const formattedMessage = `${requestInfo}
|
||||
|
||||
${text}`;
|
||||
|
||||
const telegramToken = env.TELEGRAM_TOKEN;
|
||||
const telegramChatId = env.TELEGRAM_CHAT_ID;
|
||||
|
||||
if (!telegramToken || !telegramChatId) {
|
||||
return new Response('Missing TELEGRAM_TOKEN or TELEGRAM_CHAT_ID environment variables', {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const telegramUrl = `https://api.telegram.org/bot${telegramToken}/sendMessage`;
|
||||
const telegramPayload = {
|
||||
chat_id: telegramChatId,
|
||||
text: formattedMessage,
|
||||
parse_mode: 'HTML'
|
||||
};
|
||||
|
||||
const telegramResponse = await fetch(telegramUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(telegramPayload)
|
||||
});
|
||||
|
||||
const telegramResult = await telegramResponse.json();
|
||||
|
||||
if (!telegramResponse.ok) {
|
||||
return new Response(JSON.stringify({
|
||||
success: false
|
||||
}), {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return new Response(JSON.stringify({
|
||||
success: true
|
||||
}), {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
return new Response(JSON.stringify({
|
||||
success: false
|
||||
}), {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -1,40 +0,0 @@
|
||||
import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import worker from '../src';
|
||||
|
||||
describe('Telegram Worker', () => {
|
||||
it('rejects GET requests', async () => {
|
||||
const request = new Request('http://example.com');
|
||||
const ctx = createExecutionContext();
|
||||
const response = await worker.fetch(request, env, ctx);
|
||||
await waitOnExecutionContext(ctx);
|
||||
expect(response.status).toBe(405);
|
||||
expect(await response.text()).toMatchInlineSnapshot(`"Method not allowed"`);
|
||||
});
|
||||
|
||||
it('requires text parameter in JSON POST', async () => {
|
||||
const request = new Request('http://example.com', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({})
|
||||
});
|
||||
const ctx = createExecutionContext();
|
||||
const response = await worker.fetch(request, env, ctx);
|
||||
await waitOnExecutionContext(ctx);
|
||||
expect(response.status).toBe(400);
|
||||
expect(await response.text()).toMatchInlineSnapshot(`"Missing text parameter"`);
|
||||
});
|
||||
|
||||
it('requires environment variables', async () => {
|
||||
const request = new Request('http://example.com', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ text: 'Hello World!' })
|
||||
});
|
||||
const ctx = createExecutionContext();
|
||||
const response = await worker.fetch(request, {}, ctx);
|
||||
await waitOnExecutionContext(ctx);
|
||||
expect(response.status).toBe(500);
|
||||
expect(await response.text()).toMatchInlineSnapshot(`"Missing TELEGRAM_TOKEN or TELEGRAM_CHAT_ID environment variables"`);
|
||||
});
|
||||
});
|
||||
@@ -1,11 +0,0 @@
|
||||
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config';
|
||||
|
||||
export default defineWorkersConfig({
|
||||
test: {
|
||||
poolOptions: {
|
||||
workers: {
|
||||
wrangler: { configPath: './wrangler.jsonc' },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -1,43 +0,0 @@
|
||||
/**
|
||||
* For more details on how to configure Wrangler, refer to:
|
||||
* https://developers.cloudflare.com/workers/wrangler/configuration/
|
||||
*/
|
||||
{
|
||||
"$schema": "node_modules/wrangler/config-schema.json",
|
||||
"name": "miti-telegram",
|
||||
"main": "src/index.js",
|
||||
"compatibility_date": "2025-08-31",
|
||||
"observability": {
|
||||
"enabled": true
|
||||
}
|
||||
/**
|
||||
* Smart Placement
|
||||
* Docs: https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement
|
||||
*/
|
||||
// "placement": { "mode": "smart" }
|
||||
/**
|
||||
* Bindings
|
||||
* Bindings allow your Worker to interact with resources on the Cloudflare Developer Platform, including
|
||||
* databases, object storage, AI inference, real-time communication and more.
|
||||
* https://developers.cloudflare.com/workers/runtime-apis/bindings/
|
||||
*/
|
||||
/**
|
||||
* Environment Variables
|
||||
* https://developers.cloudflare.com/workers/wrangler/configuration/#environment-variables
|
||||
*/
|
||||
// "vars": { "MY_VARIABLE": "production_value" }
|
||||
/**
|
||||
* Note: Use secrets to store sensitive data.
|
||||
* https://developers.cloudflare.com/workers/configuration/secrets/
|
||||
*/
|
||||
/**
|
||||
* Static Assets
|
||||
* https://developers.cloudflare.com/workers/static-assets/binding/
|
||||
*/
|
||||
// "assets": { "directory": "./public/", "binding": "ASSETS" }
|
||||
/**
|
||||
* Service Bindings (communicate between multiple Workers)
|
||||
* https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings
|
||||
*/
|
||||
// "services": [{ "binding": "MY_SERVICE", "service": "my-service" }]
|
||||
}
|
||||
3
wrangler.toml
Normal file
3
wrangler.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
name = "miti-telegram"
|
||||
main = "index.js"
|
||||
compatibility_date = "2025-08-31"
|
||||
Reference in New Issue
Block a user