Comments (6)
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.
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:
Seems like something wrong with your code, not the gem.
from telegram-bot-ruby.
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.
Sorry, pressed the wrong button and reopened. lol
from telegram-bot-ruby.
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.
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)
- Telegram new launch: Web_app HOT 9
- AnswerInlineQuery not work correctly HOT 2
- undefined method `from' for nil:NilClass (NoMethodError) HOT 3
- How to read channel messages?
- Forward message with images HOT 1
- User chats HOT 1
- Get list of join request to channel
- ArgumentError if message is not a string HOT 3
- Question. How do i listen only new message after bot was started? HOT 2
- How to receive Voice messages? HOT 1
- Periodic Net::ReadTimeout errors causing restart for 24/7 Bot HOT 3
- Support for ChatMember types HOT 4
- Add VCR for specs HOT 1
- `ForceReply#force_reply` type and optionality HOT 1
- Return defined types (Ruby objects) HOT 3
- Update `thumb` fields to `thumbnail`
- Webhooks empty ChatMember HOT 2
- TERM trap does not work anymore as in README HOT 5
- Uploading large files with local API causes Net::ReadTimeout with #<TCPSocket:(closed)> HOT 1
- Message.text is nil even thought the schema says it can not be HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from telegram-bot-ruby.