Bot Telegram php: Conversation with a specific user - php

After this step:
if($message == "Bot copy me")
{
sendMessage($chatid, "Sure?");
}
How a bot can reply to this specific user that said "Bot copy me" if he say "yes", for example, and copy him until he say "stop" ?

You have to use your own database. If you don't have one, the easiest way is use file.
For instance, use file_get_contents("/tmp/tg-bot-{$chatid}-status") to get status. When received Sure, use file_put_contents("/tmp/tg-bot-{$chatid}-status", "REPLY") to set.

Related

How can wait my Telegram Bot for a incoming message?

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) [...]

How can I differentiate between a 'Message' update and a 'Callback Query' update? (Telegram Bot API)

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.

create rtmp stream after approval in flash

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;
}
}

How to user predis for publish more than one time

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.

giving users invites?

I want people to request invites to use my app, they fill in their email address, but how can generate an url for them to go to register? Presumably only to be used once! I was thinking some sort of md6 code using php!
P.S. If there is a tutorial for this, please give a the link. Thanks.
In the email you use a random code, for example (a part of) the session-id of the user
<?php
$code = session_id();
You save this code somewhere in your database and when the user hits your register page (for example, http://mydomain.tld/register?code=here-register-code), you do something like this:
<?php
$code = $_GET['code'];
if (my_code_is_valid($code) {
echo 'Hey, you are able to register now!';
} else {
echo 'Sorry friend, you need an invite first!';
}
You need to define your my_code_is_valid() function, but that's depending on your own code (framework, database system etc).

Categories