question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webhook working with one bot but not another.

See original GitHub issue

Context

I can run a barebones bot using my webhook and all works perfectly fine:

index.js

const Telegraf = require('telegraf')
const express = require('express')
const expressApp = express()

const bot = new Telegraf('KEY HERE')


if(process.env.NODE_ENV !== 'production') {
bot.telegramdeleteWebhook()
bot.startPolling()
} else {
expressApp.use(bot.webhookCallback('/CM'))
bot.telegram.deleteWebhook()
bot.telegram.setWebhook('https://bots.xxxx.com/CM')
}

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'))
bot.hears(/buy/i, (ctx) => ctx.reply('Buy-buy'))

expressApp.get('/', (req, res) => {
  res.send('Hello World!')
})

expressApp.listen(8071, () => {
  console.log('Example app listening on port 8071!')
})

bot.catch((err) => {
  console.log('Ooops', err)
})

As you can see I have an IF statement to select whether running in production or not.

When trying to implement the same methods in my actual bot project, I see the logger receiving the messages from the telegram user, but the replies are not being processed. It’s as if the bot can see the inbound message but does not do anything after this.

My file structure is:

Paste 1: App/Bot/index.js


const Telegraf = require('telegraf');
const API_KEY = require('../../Config').envs[process.env.NODE_ENV].telegramBot.API_TOKEN;
const user = require('./Middleware/storeUser');
const filters = require('./Middleware/Filters');
const log = require('../../Utilities/Logger');
const express = require('express');

// Middleware dependencies
const TelegrafLogger = require('telegraf-logger');
const commandParts = require('telegraf-command-parts');

// Initialise classes
const telegramBot = new Telegraf(API_KEY);
const telegramBotLogger = new TelegrafLogger();

// Set username
telegramBot.telegram.getMe().then((botInfo) => {
  telegramBot.options.username = botInfo.username;
});

// Initialise Middleware
telegramBot.use(telegramBotLogger);
telegramBot.use(user());
telegramBot.use(filters());
telegramBot.use(commandParts());

telegramBot.catch((err) => {
  log.error(err);
});


// Webhook setup
const expressApp = express();

expressApp.get('/', (req, res) => {
  res.send('Hello World!');
});

expressApp.listen(8071, () => {
  log.debug('Example app listening!');
});

if (process.env.NODE_ENV === 'production') {
  expressApp.use(telegramBot.webhookCallback('/CM'));
  telegramBot.telegram.setWebhook('https://bots.xxxxxxx.com/CM');
}

module.exports = telegramBot;

Paste 2: App/index.js - The main program starts here.

const log = require('../Utilities/Logger');
const botClient = require('./Bot');
const Helpers = require('./Bot/Helpers');

function startApp() {
  log.info(`Starting environment: ${process.env.NODE_ENV}`);
  if (process.env.NODE_ENV !== 'production') {
  // Start polling as we are not in production
    botClient.telegram.deleteWebhook();
    botClient.startPolling();
  } 
  // Load plugin modules
  Helpers.loadPlugins();
  return log.info('App loaded.');
}

startApp();

An example of the logs:

[2018-05-16T04:43:05.067] [INFO] default - Starting environment: production  - [                                                                                                                                                             ../../App/index.js:6:7]
[2018-05-16T04:43:05.085] [INFO] default - App loaded.  - [../../App/index.js:12                                                                                                                                                             :14]
message => @munsd Chris (3434234324): <text> /start
message => @munsd Chris (3434234324): <text> /test
message => @munsd Chris (3434234324): <text> hi

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
Otto-AAcommented, May 16, 2018

Maybe the problem is, that one of the middlewares doesn’t return next()? For example, this works:

bot.use((ctx, next) => {
  console.log(ctx);
  return next();
});
bot.start(ctx => ctx.reply('Hello World'));

while this doesn’t:

bot.use((ctx, next) => {
  console.log(ctx);
});
bot.start(ctx => ctx.reply('Hello World'));

So I would suggest to look if (1) every middleware get’s called and (2) they all return next()

0reactions
ImTheDevelopercommented, May 16, 2018

Ignore - my bad on this one. Lots of return statements missing on functions 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

Triggering a bot with a webhook from another system (Thinkific)
I have a learning management system (LMS) that has a webhook that sends a POST to a URL with a JSON payload. I'd...
Read more >
Unable to send a webhook only in one server - Stack Overflow
Webhooks are enabled in the intents and the text channel. This exception is only being raised in one server, it works fine in...
Read more >
Make a bot for simple interactions in Teams using a Webhook
The solution is a Webhook in Teams and a Bot (surprised?). This works as ... It will not work with branching dialogs, or...
Read more >
Messages not reaching my bot - Cisco Community
This sounds like an issue with your webhook. I presume you have a messages:created webhook, was this created with the bot token?
Read more >
How To Create a Bot That Sends Multiple Webhook ...
How To Create a Bot That Sends Multiple Webhook Responses Via One Trigger · This tutorial still requires an admin to trigger the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found