planetteamspeak / ts3phpframework

Modern use-at-will framework that provides individual components to manage TeamSpeak 3 Server instances
https://www.planetteamspeak.com
GNU General Public License v3.0
211 stars 59 forks source link

Keep the bot connected waiting for requests #125

Closed Aldallo closed 5 years ago

Aldallo commented 5 years ago

Hello, I'd like to know if it's possible to keep the query user connected and waiting for requests, instead of disconnecting after the request is done

I tried waiting for an event and sending a request at the same time, I couldn't figure it out

Thanks in advance

Sebbo94BY commented 5 years ago

Yes, it is possible. I've already played a bit with it, but I hadn't time to create a really simple bot (eg. afk mover) yet. I wanted to create such a simple bot in order to add it to the documentation.

I've used the example from the online documentation, subscribed to one more event and added a callback function for this event:

12. Create a Simple Bot waiting for Events

// load framework files
require_once("libraries/TeamSpeak3/TeamSpeak3.php");

// connect to local server in non-blocking mode, authenticate and spawn an object for the virtual server on port 9987
$ts3_VirtualServer = TeamSpeak3::factory("serverquery://username:password@127.0.0.1:10011/?server_port=9987&blocking=0");

// get notified on incoming private messages
$ts3_VirtualServer->notifyRegister("textprivate");

// register a callback for notifyTextmessage events 
TeamSpeak3_Helper_Signal::getInstance()->subscribe("notifyTextmessage", "onTextmessage");

// register a callback for serverqueryWaitTimeout events
TeamSpeak3_Helper_Signal::getInstance()->subscribe("serverqueryWaitTimeout", "onTimeout");

// wait for events
while(1) $ts3_VirtualServer->getAdapter()->wait();

// define a callback function for the text message
function onTextmessage(TeamSpeak3_Adapter_ServerQuery_Event $event, TeamSpeak3_Node_Host $host)
{
    echo "Client " . $event["invokername"] . " sent textmessage: " . $event["msg"];
}

// define a callback function for the timeout
function onTimeout($timeout, \TeamSpeak3_Adapter_ServerQuery $event)
{
    // By default, the query times out after 5 minutes (300 seconds)
    // Send a command to reset the timeout timer
    if ($serverQuery->getQueryLastTimestamp() < time() - 290)
    {
        $event->request("clientupdate");
    }
}
Aldallo commented 5 years ago

Thank you for the quick reply but I think I didn't make my question clear, I want to keep the bot connected by a bot.php for example

and then in index.php, after a button is clicked for example, I want the bot to send a message to the visitor

I made this by separate files (bot.php and index.php), but the bot immediately disconnects after the request is done, I want to keep the bot connected and waiting for request if that is possible.

Using your code for a bot.php connects once per request, and then keeps connected but unusable. so another query user connects to perform the other request

Thanks again

Aldallo commented 5 years ago

Bump

ronindesign commented 5 years ago

@Aldallo you need to add the functions / logic as you require it.

The bot example above can have callbacks filled out for handling the different query events.

This includes textmessage, where you could send commands via chat / private message in TeamSpeak.

Otherwise, if you want to send commands from an external source, such as your index.php, you need to build in your own client/server protocol for the bot.php to listen for external requests, for example via socket, and then your index.php script could connect to the bot.php listening socket and deliver a request whenever you click a button on the web page, etc.

However, running the server query bot and listening on a socket for external commands from index.php is not necessarily a trivial thing to construct and will depend on your unique situation. You'd need to use some form of looping with socket selection or multi-threading (p-threads, etc) to have these running in the same file, e.g. bot.php. PHP.net has an example from user vardhan of managing mutliple socket connections: https://www.php.net/manual/en/function.socket-select.php#56241

Again, we won't be able to just write this code for you as it will be highly dependent on you use case scenarios, but if you get started and need code comments / suggestions along the way, I'm sure the community can help out either in the forums or here.

I'll close this ticket for now as there's not much else we can do to help here, but please feel free to comment further or open a new ticket as needed.