Tsuk1ko / cfworker-middleware-telegraf

Make telegraf (a telegram bot framework) useable in Cloudflare Workers
https://www.npmjs.com/package/cfworker-middleware-telegraf
MIT License
64 stars 14 forks source link

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

Open sparkyfen opened 2 years ago

sparkyfen commented 2 years ago

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?