Package: anonchat
anonchat-api.pyPython implementation of anonchat APIInstallation$pipinstallanonchatBuildingYou need to sync up this repo using git:https://github.com/anonchat-org/anonchat-api.pyFirst of all, install 'build' package$py-mpipinstall--upgradebuildThen, build this package. You need to be in root of directory, not in src and etc.$py-mbuildIt will give you two packages:dist/
  anonchat-0.0.1-py3-none-any.whl
  anonchat-0.0.1.tar.gzInstall builded package:pip install ./dist/anonchat-0.0.1-py3-none-any.whlUsageImport all from anonchat.clientfromanonchat.clientimport*You can check version, if you want.print(AnonClient._VERSION)Now, lets create your client.Basic events and connectionAnonClient(ip: str, - The IP of the server
port: int, - Port of the server
name: str - Bot name
)bot=AnonClient("IP",port,"ExampleNickname")# Change this to your info!By default, bot uses API v2. API v1 is supported, but not tested.
To change API version, after bot creation, change this:bot.version=2# Set API 2. This is set by default.# And to API 1bot.version=1# Set deprecated API 1.Let's send message about bot connection. You need this decorator:@bot.event_connect@bot.event_connectdefon_connect():print(f"Bot{bot.username}connected!")You can send messages with function bot.sendbot.send(
text: str
)Add this to our on_connect function!@bot.event_connectdefon_connect():print(f"Bot{bot.username}connected!")bot.send("I am connected!")This function is called when bot is fully connected.If you want to send message on bot disconnect, you can also add this to your code.@bot.event_disconnect@bot.event_disconnectdefon_disconnect():bot.send("See you next time!")print("Bot disconnecting...")This function will be called before bot disconnect, so you can send messages.Thats all. Lets connect our bot. Write this function after ALL code. Or, it won't be called.bot.connect()So, our bot is working. It can be disconnected using another function.It it normal, if you get an error here. This is because of closed socket.bot.close()Lets go to another part.Message processingIf you want to get all messages, set your custom message event function.@bot.event_message@bot.event_messagedefon_message(message):All messages, which passed to on_message, will be V1Message or V2Message class objects, depending on the selected API versionV2MessageVariables:.contents: str - Message contents.author: str - Message author.time: datetime.now - Time, when message was recieved by client..me: bool - Is this my message? But, this is not accurate, because anyone can set your name..bot: class  - The bot object.Functions:.reply(text: str - Reply text
) - Reply to messageCan be converted to:bytes - Dumped Encoded JSONstr - Dumped JSONMessage author is not availible on API1, so there is no .authorV1MessageVariables:.contents: str - Message contents.time: datetime.now - Time, when message was recieved by client..me: bool - Is this my message? But, this is not accurate, because anyone can set your name..bot: class  - The bot object.Functions:.reply(text: str - Reply text
) - Reply to messageCan be converted to:bytes - Encoded message.contentsstr - message.contentsIf the server (as the server on Dart does) sends a message to a client with API 2 of API 1 standard, the client will automatically adapt it to API 2 and the one who sent the message will be named "V1-Package". Since the function which adjusts for API 2 is local, it is possible to change the name to something else:bot.v1_client="V1MSG"# Or something else, if you like.If the official Python server is used, the server will do it automatically by itself, with exactly the same name "V1-Package", it cannot be changed.Next code is only for API2.Lets write basic on_message function, which will be detecting, if there is 'Hello' at start, and if message is not from our bot.@bot.event_messagedefon_message(message):ifmessage.contents.startswith("Hello")andnotmessage.me:# If message has 'Hello' at start, and this is not our message.message.reply(f"Hello, dear{message.author}!")# Reply to message.This function will be called all time when the message is recieved.API1/API2 CodeIf you want to do some processing before message sending, there is also a function.@bot.event_sendThis function is called before message send, so it uses another objects.RequestV2MessageVariables:.contents: str - Message contents.author: str - Message authorCan be converted to:bytes - Dumped Encoded JSONstr - Dumped JSONRequestV1MessageVariables:.contents: str - Message contentsCan be converted to:bytes - Encoded message.contentsstr - message.contentsThere is no .bot, .me, .time and .reply, because this message is not sent. Of course, it is our message.And example code:@bot.event_senddefon_send(message):print(f"Bot will send message with text '{message.contents}'")ErrorsThere is three type of errors you can get.anonchat.SendErrorYou can get this while sending message in closed/disconnected socket.anonchat.SocketErrorYou can get this while trying to connect to bad server adress or offline server.RuntimeErrorYou can get this if there is an error in your code.Good luck!Thats all you need to know.
This example can be found in examples dir
Good luck in writing bot/client for your server!