Code Monkey home page Code Monkey logo

Comments (6)

kosmodrey avatar kosmodrey commented on September 25, 2024

Hi,

At this moment Telegram Bot API does not support buttons with custom callbacks. You can use inline buttons with "cmdbutton" module instead.

from telebot.

williamsonego avatar williamsonego commented on September 25, 2024

Thanks for return = )

It would be possible to use the cmdButton module with the buttons without inline ?

from telebot.

kosmodrey avatar kosmodrey commented on September 25, 2024

Unfortunate, no. Keyboard buttons do not have callbacks (only sends a text message when the button is pressed).

But you can do something like this:

bot.on('/start', msg => {
  // Keyboard markup
  let markup = bot.keyboard([
    ['Button1'],
  ], { resize: true });
  return bot.sendMessage(msg.from.id, 'Keyboard button.', { markup });
});

bot.on('text', (msg, props) => {
  // Button name text
  if (msg.text == 'Button1') {
    // Call /hello command
    return bot.event('/hello', msg, props);
  }
  return bot.sendMessage(msg.from.id, msg.text);
});

bot.on('/hello', msg => {
  return bot.sendMessage(msg.from.id, 'Hello, Command!');
});

from telebot.

williamsonego avatar williamsonego commented on September 25, 2024

kosmodrey again thank you for your attention ... now , as I would to enter a sub- menu buttons ?

EX : button1 >> by clicking the button appear the buttons 2 and 3

from telebot.

kosmodrey avatar kosmodrey commented on September 25, 2024

You can do something like this:

// Navigation button names
const mainBtn = 'Open sub-buttons';
const backBtn = 'Back to main menu';

// Main menu buttons
const mainButtons = bot.keyboard([
    [mainBtn]
  ], { resize: true });

// Sub-menu buttons
const subButtons = bot.keyboard([
    ['Button1', 'Button2'],
    [backBtn]
  ], { resize: true });

// On start, send a main menu markup
bot.on('/start', msg => {
  let markup = mainButtons;
  return bot.sendMessage(msg.from.id, 'Hello!', { markup });
});

// On text, check if it's a button name
bot.on('text', msg => {
  if (msg.text == mainBtn) {
    // Main button clicked, send a sub-menu markup
    return bot.sendMessage(msg.from.id, 'Sub-menu', { markup: subButtons });
  } else if (msg.text == backBtn) {
    // Back button clicked, send a main menu markup
    return bot.sendMessage(msg.from.id, 'Main menu', { markup: mainButtons });
  }
});

from telebot.

williamsonego avatar williamsonego commented on September 25, 2024

Perfect! It Works very well =)

Thanks a lot kosmodrey

from telebot.

Related Issues (20)

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.