I'm trying to query some data from my database. there is three option for searching data from database
User id
Phone no.
E-mail
I've created an inline keyboard by choosing a search option from the above list
Screenshot of bot chat
I'm looking for a solution where if I choose user_id then the bot will ask me to insert user_id. And will accept the only user_id as input. otherwise, it will return with an error message.
if I choose email from the inline keyboard at that time bot will accept only email that time. other bot commands will not be accepted until any error or success is returned from the bot.
I'm using Laravel as my backend.
You should have a column called state for each user_id.
And when you receive any message you should check the state before responding.
So, This is pseudo-code:
$Bot = new TelegramBot(getenv('BOT_TOKEN'));
function MessageHandler($message)
{
$state = DB::table('users')->row(['user_id' => $message->from->id])->column('state');
if ($state === false)
{
# Create new row for this user..
}
switch ($state)
{
case 'SEARCHING_BY_ID':
# Check if message is number
break;
case 'SEARCHING_BY_PHONE':
# Check if message is phone number
break;
case 'SEARCHING_BY_MAIL':
# Check if message is E-mail
break;
default:
# Now you should process by any command
break;
}
}
$Bot->OnMessage(MessageHandler);
Related
How can I let interact my Telegram Bot with Users? E.g.:
User: /buy
Bot: What do you want to buy?
User: Icecream
Bot: You have successfully bought Icecream!
So how can I do this?
switch($message) {
[...]
case "/buy":
sendMessage($chatID, "What do you want to buy?");
//Here I need your help :D
break;
}
Assuming you are using a webhook to receive updates, your php script runs again on every update you are receiving.
In this case, you will need to save a certain "status" of the user which you are checking everytime your bot receives a message to indicate what you have to do next.
An example would be:
switch($message) {
case "/buy":
sendMessage($chatID, "What do you want to buy? Icecream, Water or Sprite?");
$storage[$chatID]['status'] = 'awaitingProductChoice';
saveStorage();
break;
}
You need to save $storage[$userId] somehow (saveStorage();). Ideally would be to use a database, if you haven't got one, use file_put_contents('storage.json', json_encode($storage)); or something similar to serialize it. SESSIONS won't work, since Telegram Servers do not send cookies.
Then place some similar code before your switch statement:
$storage = readStorage(); // load from DB or file_get_contents from file
if ($storage[$chatID]['status'] === 'awaitingProductChoice') {
// return " You have successfully bought Icecream!"
$storage[$chatID]['product choice'] = $message['text'];
} else if ($storage[$chatID]['status'] === 'other stuff') {
// other step
}
else switch($message) [...]
Sorry if my question gets too messy, I'm new here, so, any advice is welcome.
How can I differentiate between a 'Message' update and a 'Callback Query' update?
I've managed to make an inline keyboard, but when I use it, the bot just hangs, he doesn't reply anything. I did a little bit of research and found this question, which helped me understand the problem, but not much else.
My bot uses something like this right now:
// read incoming info and grab the chatID
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
switch($update["message"]["text"]){
/* insert magic here */
}
So, this code can handle Messages, but not CallbackQueries. If I wantew to handle them, I could use something like this (based on the other question's answer):
$callback_query = $update["callback_query"]
/* same as above */
But how can I check whether it is a message or a callback query?
if (($update['message']) != null) {
} else if ($update['callback_query'] != Null) {
According to telegram Docs:
At most one of the optional parameters can be present in any given
update.
so you just need to check which one of them is not Null.
You can simply check if CallbackQuery is null or not.
See the Telegram docs:
CallbackQuery
This object represents an incoming callback query from a callback
button in an inline keyboard. If the button that originated the query
was attached to a message sent by the bot, the field message will be
present. If the button was attached to a message sent via the bot (in
inline mode), the field inline_message_id will be present. Exactly one
of the fields data or game_short_name will be present.
Is it possible to create a netstream after the user has approved to the use off the cam?
I ask this because i want to be able to detect if a user is actually transmitting so other people can see that their cam is on.
Right now the stream gets created after hitting play while approval has not be given yet.
Is it perhaps possible to send something along with hitting the allow/deny?
I'm using AS2 with RED5.
found the answer:
Cam.onStatus = function(infoObj:Object) {
switch (infoObj.code) {
case 'Camera.Muted' :
trace("Camera access is denied");
break;
case 'Camera.Unmuted' :
send_data.camuser =user;
send_data.camon = "PRIVATE";
break;
}
}
I Learning a framework Kohana.
What is difference between Kohana::message and Kohana::config?
They perform the same function.
Maybe if there is a difference between the concept?
Kohana:message
These are generally used to store messages that will be displayed to the user. For example if you have a method that tries to create a user and it fails, you can get the relevant you can you may have the following in a User Controller:
$create = $this->create_user($user);
if($create)
{
// user created
$message = Kohana::message('user', 'create_success');
}
else
{
// failed to create user
$message = Kohana::message('user', 'create_error');
}
Kohana:config
This is used for configuration information such as the hash_method used in auth module and you can access it using Kohana::$config->load('auth.hash_method')
One is for configuration information. The other is for reusable text: Kohana::message('registration.error.email') could say something like "There is already an account using the email address you entered, an email with instruction on how to reset you password has been sent in case you forgot it.".
How can I publish info between the clients more than once?
I mean when I publish info from one user to other, he receives and backwards, but this is only once.
Because when one user send something to the other, the GET is being loaded and the receiving stops, how can I make it that way so the clients receives forever, not only once?
How pub/sub works: like a channel, you put from one side and you get the same from the other side.
So publisher data will be received only when there is some subscriber for it.
Use pubSub context and subscribe to a channel say "x" and from another side, keep taking the data, say from User, and publish it using publish command every time to the same channel.
Subscriber:
$redis = new Predis\Client(// put setting here, if req);
$pubsub = $redis->pubSub();
$pubsub->subscribe($channel1);
foreach ($pubsub as $message)
{
switch ($message->kind) {
case 'subscribe':
echo "Subscribed to {$message->channel}\n";
break;
case 'message':
// do something
break;
}
}
Publisher:
while(1) // or whatever condition
{
$redis->publish($channel2, $userdata);
}
You can use chat messages to break the connection, e.g. publish exit and check at subscriber if exit then close the connection and then check at publisher side if no subscriber attached, close it too.