-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #1
- Loading branch information
Showing
14 changed files
with
1,987 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
package-lock.json | ||
yarn.lock | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Middleware } from '@cfworker/web'; | ||
import { Telegraf } from 'telegraf'; | ||
|
||
declare function telegrafMiddware(bot: Telegraf): Middleware; | ||
|
||
export = telegrafMiddware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require('abortcontroller-polyfill/dist/polyfill-patch-fetch'); | ||
require('./urlPatch'); | ||
const telegrafResponseBuilder = require('./telegrafResponseBuilder'); | ||
|
||
/** | ||
* @param {import('telegraf').Telegraf} bot | ||
*/ | ||
module.exports = bot => { | ||
return async ({ req, res }) => { | ||
await bot.handleUpdate(await req.body.json(), telegrafResponseBuilder(res)); | ||
res.status = 200; | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
/** | ||
* @param {import('@cfworker/web').ResponseBuilder} res | ||
*/ | ||
module.exports = res => { | ||
return new Proxy( | ||
Object.assign(res, { | ||
set: (...args) => res.headers.set(...args), | ||
header: res.headers, | ||
}), | ||
{ | ||
set(obj, prop, value) { | ||
if (prop === 'body' && ['Object', 'Array'].includes(Object.getPrototypeOf(value).constructor.name)) { | ||
obj.body = JSON.stringify(value); | ||
obj.headers.set('content-type', 'application/json'); | ||
return true; | ||
} | ||
return Reflect.set(...arguments); | ||
}, | ||
} | ||
); | ||
let writableEnded = false; | ||
const modRes = Object.assign(res, { | ||
headersSent: false, | ||
setHeader: (name, value) => res.headers.set(name, value), | ||
end: data => { | ||
if (writableEnded) return; | ||
res.body = data; | ||
writableEnded = true; | ||
}, | ||
}); | ||
Object.defineProperty(modRes, 'writableEnded', { | ||
get: () => writableEnded, | ||
}); | ||
return modRes; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('url').URL = URL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { Telegraf } = require('telegraf'); | ||
const { Application, Router } = require('@cfworker/web'); | ||
const createTelegrafMiddware = require('../'); | ||
|
||
const bot = new Telegraf(BOT_TOKEN); | ||
bot.start(ctx => ctx.reply('Welcome')); | ||
bot.help(ctx => ctx.reply('Send me a sticker')); | ||
bot.on('sticker', ctx => ctx.reply('👍')); | ||
bot.hears('hi', ctx => ctx.reply('Hey there')); | ||
|
||
const router = new Router(); | ||
router.post(`/${SECRET_PATH}`, createTelegrafMiddware(bot)); | ||
new Application().use(router.middleware).listen(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require('dotenv').config(); | ||
const { Telegraf } = require('telegraf'); | ||
const bot = new Telegraf(process.env.BOT_TOKEN); | ||
|
||
(async () => { | ||
// set webhook | ||
await bot.telegram.setWebhook(`${process.env.BOT_WEBHOOK}/${process.env.SECRET_PATH || ''}`); | ||
|
||
// delete webhook | ||
// await bot.telegram.deleteWebhook(); | ||
|
||
// get webhook info | ||
await bot.telegram.getWebhookInfo().then(console.log); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require('dotenv').config(); | ||
const path = require('path'); | ||
const { DefinePlugin } = require('webpack'); | ||
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin'); | ||
|
||
module.exports = { | ||
entry: path.resolve(__dirname, 'index.js'), | ||
target: 'webworker', | ||
output: { | ||
filename: 'worker.js', | ||
path: path.resolve(__dirname, 'dist'), | ||
}, | ||
mode: 'production', | ||
resolve: { | ||
fallback: { | ||
fs: false, | ||
}, | ||
}, | ||
plugins: [ | ||
new NodePolyfillPlugin(), | ||
new DefinePlugin({ | ||
BOT_TOKEN: JSON.stringify(process.env.BOT_TOKEN || ''), | ||
SECRET_PATH: JSON.stringify(process.env.SECRET_PATH || ''), | ||
}), | ||
], | ||
optimization: { | ||
minimize: false, | ||
}, | ||
performance: { | ||
hints: false, | ||
}, | ||
}; |
Oops, something went wrong.