Coder Social home page Coder Social logo

tsuk1ko / cfworker-middleware-telegraf Goto Github PK

View Code? Open in Web Editor NEW
59.0 59.0 14.0 52 KB

Make telegraf (a telegram bot framework) useable in Cloudflare Workers

Home Page: https://www.npmjs.com/package/cfworker-middleware-telegraf

License: MIT License

JavaScript 100.00%
cfworker cloudflare cloudflare-workers telegraf telegram telegram-bot

cfworker-middleware-telegraf's Issues

The "chunk" argument must be one of type string or Buffer. Received type object

The caption command fails below in CFWorker with error:

  (error) TypeError: The "chunk" argument must be one of type string or Buffer. Received type object
    at new n2 (worker.js:16656:23)
    at worker.js:17181:95
    at A.write (worker.js:17182:10)
    at A.end (worker.js:17216:144)
    at s.addPart (worker.js:20412:73)
    at S (worker.js:20325:21)
    at async worker.js:20313:22
    at async Promise.all (index 2)
    at async _ (worker.js:20318:16)
    at async s.callApi (worker.js:20362:30)

Borrowed from https://github.com/telegraf/telegraf/blob/302a46d144993be7a95c5864c87a8d7b79230d1e/docs/examples/keyboard-bot.js#L83

import { Telegraf, Markup } from 'telegraf';
import { Application, Router } from '@cfworker/web';
import createTelegrafMiddware from 'cfworker-middware-telegraf';
import { DEFAULT_CORS_HEADERS } from './utils';

const bot = new Telegraf(TELEGRAM_API_TOKEN);

//... other commands here

bot.command('caption', (ctx) => {
  return ctx.replyWithPhoto({ url: 'https://picsum.photos/200/300/?random' },
    {
      caption: 'Caption',
      parse_mode: 'Markdown',
      ...Markup.inlineKeyboard([
        Markup.button.callback('Plain', 'plain'),
        Markup.button.callback('Italic', 'italic')
      ])
    }
  )
});

const router = new Router();

// Simple CORS middleware.
const cors = async ({ res }, next) => {
  for (const HEADER_KEY in DEFAULT_CORS_HEADERS) {
    res.headers.set(HEADER_KEY, DEFAULT_CORS_HEADERS[HEADER_KEY]);
  }
  await next();
};

router.post(`/${TELEGRAM_SECRET_PATH}`, createTelegrafMiddware(bot));
new Application().use(cors).use(router.middleware).listen();

Any suggestions?

Error: globalThis.XMLHttpRequest is not a constructor

Trying to use with wrangler v2

const { Telegraf } = require('telegraf');
const { Application, Router } = require('@cfworker/web');
const createTelegrafMiddleware = require('cfworker-middleware-telegraf');

const bot = new Telegraf("MY_TOKEN");
const router = new Router();
router.post(`/SECRET`, createTelegrafMiddleware(bot));
new Application().use(router.middleware).listen();

running wrangler dev results in Error: globalThis.XMLHttpRequest is not a constructor

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.