How to keep python 3 script running (Bot)

(Not native English, sorry, maybe broken English. I'm also new to programming).
Hi, I am trying to connect to a TeamSpeak server using QueryServer to make a bot. After a few days fighting it ... it works, with only one problem, and I'm stuck with that.

If you need to check, this is the TeamSpeak API I use: http://py-ts3.readthedocs.org/en/latest/api/query.html

And this is a summary of what is actually happening in my script:

  • He is connecting.
  • It checks the channel identifier (and the client’s own identifier)
  • He joins the channel
  • Script terminates, so it shuts down.

My question is: how can I do this, it doesn't turn off? How can I make a script stand by so that it can read if someone types "hi bot" in a channel? All the code necessary for reading texts and answering them seems easy to program, however, I face a problem when I can not support the "launch" of the bot, because it closes the file as soon as it finishes working with the script.

Additional Information:
I am using Python 3.4.1 .
I tried to learn Threading http://www.tutorialspoint.com/python/python_multithreading.htm , but either M'm is dumb or it doesn't work like me. There is a function in the API called on_event that I would like to keep working all the time. The bot code should be run only once, and then remain β€œpending” until an event occurs. How can I do it? There is no clue.

The code:

 import ts3 import telnetlib import time class BotPrincipal: def Conectar(ts3conn): MiID = [i["client_id"] for i in ts3conn.whoami()] ChannelToJoin = "[Pruebas] Bots" ts3conn.on_event = BotPrincipal.EventHappened() try: BuscandoIDCanal = ts3conn.channelfind(pattern=ChannelToJoin) IDCanal = [i["cid"] for i in BuscandoIDCanal] if not IDCanal: print("No channel found with that name") return None else: MiID = str(MiID).replace("'", "") MiID = str(MiID).replace("]", "") MiID = str(MiID).replace("[", "") IDCanal = str(IDCanal).replace("'", "") IDCanal = str(IDCanal).replace("]", "") IDCanal = str(IDCanal).replace("[", "") print("ID de canal " + ChannelToJoin + ": " + IDCanal) print("ID de cliente " + Nickname + ": " + MiID) try: print("Moving you into: " + ChannelToJoin) ts3conn.clientmove(cid=IDCanal, clid=MiID) #entra al canal try: print("Asking for notifications from: " + ChannelToJoin) ts3conn.servernotifyregister(event="channel", id_=IDCanal) ts3conn.servernotifyregister(event="textchannel", id_=IDCanal) except ts3.query.TS3QueryError: print("You have no permission to use the telnet command: servernotifyregister") print("------- Bot Listo -------") except ts3.query.TS3QueryError: print("You have no permission to use the telnet command: clientmove") except ts3.query.TS3QueryError: print("Error finding ID for " + ChannelToJoin + ". telnet: channelfind") def EventHappened(): print("Doesn't work") # Data needed # USER = "thisisafakename" PASS = "something" HOST = "111.111.111.111" PORT = 10011 SID = 1 if __name__ == "__main__": with ts3.query.TS3Connection(HOST, PORT) as ts3conn: ts3conn.login(client_login_name=USER, client_login_password=PASS) ts3conn.use(sid=SID) print("Connected to "+HOST) BotPrincipal.Conectar(ts3conn) 
+5
source share
1 answer

With a quick look at the API, it looks like you need to explicitly tell the ts3conn object to wait for events. There seem to be several ways to do this, but ts3conn.recv(True) seems most obvious:

Blocks until all raw responses are received, or forever if recv_forever true.

Presumably as you enter each command, it will call your on_event handler, and then when you return from it, it will return to waiting forever for the next command.

I don't know if you need threads or not, but the docs for recv_in_thread make it sound like you could:

Calls recv() on the stream. This is useful if you used servernotifyregister and expect to receive events.

Presumably you want to receive both servernotify events and commands, and I assume this library is written, what do you need streams for? If so, just call ts3conn.recv_in_thread() instead of ts3conn.recv(True) . (If you look at the source , all that does is start the background thread and call self.recv(True) on that thread.)

+3
source

Source: https://habr.com/ru/post/1204939/


All Articles