Giter VIP home page Giter VIP logo

Comments (6)

atipugin avatar atipugin commented on May 20, 2024 1

I got you. You have to store somehow user's current state and decide what to do depending on this state. In your example, you need to change user's state to "adding_something" if he/she types "/help" and chooses "Add". All next incoming messages from that user should be processed according his state. So, if user with state "adding_something" types "/help" again, you have treat it as a string, not a command to show the help menu.

I did something similar here: https://github.com/atipugin/telekrug. It uses sinatra + webhooks, but in general idea is the same.

Take a look here: https://github.com/atipugin/telekrug/blob/master/app/workers/process_message.rb. This class (sidekiq worker) handles each incoming message from any user.

from telegram-bot-ruby.

atipugin avatar atipugin commented on May 20, 2024

Following code did the job perfectly:

require 'bundler/setup'
require 'telegram/bot'

token = 'secret'

Telegram::Bot::Client.run(token) do |bot|
  bot.listen do |message|
    option = 'What you wanna do?'
    answers = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: [%w(Add Remove), %w(Search)], one_time_keyboard: true)
    bot.api.send_message(chat_id: message.chat.id, text: option, reply_markup: answers)
  end
end

Proof pic:

screen shot 2015-12-07 at 00 08 27

Seems like something wrong with your code, not the gem.

from telegram-bot-ruby.

raphaelmro avatar raphaelmro commented on May 20, 2024

Yes, it works but i wanna do stg like:
When the user type '/help' the 'menu' is opened.

Telegram::Bot::Client.run(token_telegram) do |bot|
  bot.listen do |message|
    case message.text
      when '/help'
        option = 'What you wanna do?'
        answers =
            Telegram::Bot::Types::ReplyKeyboardMarkup
                .new(keyboard: [%w(Add Remove), %w(Search], one_time_keyboard: true)
        bot.api.send_message(chat_id: message.chat.id, text: option, reply_markup: answers)

...and depending the users' option, i will do an action. The problem is, how can i get the value i choose?
Example: Type '/help' > (Menu is displayed) > Choose 'Add' >(Bot asks for a string to add)> Users type>Store the string(array, hash or db)

from telegram-bot-ruby.

raphaelmro avatar raphaelmro commented on May 20, 2024

Sorry, pressed the wrong button and reopened. lol

from telegram-bot-ruby.

raphaelmro avatar raphaelmro commented on May 20, 2024

Yeah, i understood but i' having problem to get the 'Add' string when you press the option, in this case my reply to the question. A simple message.text was not supposed to get it?

  Telegram::Bot::Client.run(token_telegram) do |bot|
    bot.listen do |message|
      case message.text
        when '/help'
          option = 'What you wanna do?'
          answers =
              Telegram::Bot::Types::ReplyKeyboardMarkup
                  .new(keyboard: [%w(Add Remove), %w(Search], one_time_keyboard: true)
          bot.api.send_message(chat_id: message.chat.id, text: option, reply_markup: answers)
          my answer = message.text.. #my problem is here - not getting the 'add' reply
          case my_answer
            when "Add"
             # (do stg here)
            when "Remove"
             # (do stg here)
            when "Search"
             # (do stg here)
          end

Sorry if it's a newbie question, i'm learning ruby with this project.

from telegram-bot-ruby.

atipugin avatar atipugin commented on May 20, 2024

It doesn't work this way. You can't handle answers inside when '/help' block, because answer message never gets inside that block (as your statement condition clearly says: "only messages with '/help' text goes here").

from telegram-bot-ruby.

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.