tomer8007 / kik-bot-api-unofficial

Python API for writing unoffical Kik bots that act like humans
MIT License
127 stars 77 forks source link

Some fixes that help with useability. #239

Open TheGreatCodeholio opened 11 months ago

TheGreatCodeholio commented 11 months ago

File formatting, log formatting fixed in some files. Fixed typo requets to request

Fixed Sending Images included option allow_forward, allow_save

Added Random GIF option picks a random GIF from search.

Fixed parsing different types of group/chat messages. So you can tell where a image/sticker/gif came from.

Modified echo bot to include on temp ban received and on pong received as examples.

Added getting gateway node based on kik APK version. Instead of using an old gateway.

Added method to catch temp bans and on pong to echo bot example

tomer8007 commented 11 months ago

Thank you, this looks good

TheGreatCodeholio commented 11 months ago

Added another commit ^ just so you are aware :)

tomer8007 commented 11 months ago

Nice! :-) Video messages are a highly requested feature. How about calling it VideoMessage/video_message instead of ChatVideo\ chat_video? Also let me know when you're done with the commits here so I can merge.

TheGreatCodeholio commented 11 months ago

Okay made that change and I am done with commits for now.

tomer8007 commented 11 months ago

It still says "Sends an image chat message" inside send_video_messaage

TheGreatCodeholio commented 11 months ago

Fixed both the docstring for video message and added a new file globals to store global variables in. Currently utilized for the logger. So user can user the logger instance in the API for their bot script to unify them. Example in example_bot. Think two other small changes as well.

tomer8007 commented 11 months ago

Why do we need the ability to set logger_name in the constructor?

TheGreatCodeholio commented 11 months ago

Why do we need the ability to set logger_name in the constructor?

My thought process was a long of lines of my own bot I have created. I have multiple files and across those file I get the with getLogger(log_name)

Adding the ability to set the log name in the constructor allows the user to essentially piggy back their bot script logs on top of the logging classes I added to the API. Being able to specify the name of the logger, is somewhat essential in that process.

tomer8007 commented 11 months ago

Let's just move the log_level argument to be before the logger_name, because it's more likely to be changed.

TheGreatCodeholio commented 11 months ago

I fixed that and I made a couple small changes to improve flow.

If trying to authenticate and it fails. It now sets kik_node to None and retries connection with user/password

Device_id generation moved to init only generates if device_id is None. This save some hassle, from trying to log in without node to get an authenticated session. Prior device_id was changing every time even if it was given.

tomer8007 commented 11 months ago

If trying to authenticate and it fails. It now sets kik_node to None and retries connection with user/password

I hope you don't mean that it fails now, only that you added failure handling code

TheGreatCodeholio commented 11 months ago

If trying to authenticate and it fails. It now sets kik_node to None and retries connection with user/password

I hope you don't mean that it fails now, only that you added failure handling code

Correct, I have just added error handling.

If self.kik_node is provided, it tries to authenticated using existing session. However.... if that fails the previous behavior was to exit. It now forces a login using username/password

TheGreatCodeholio commented 11 months ago

I changed log to logger for both instances. self.log and log = so now they are self.logger and logger respectively.

Added URI check for NoneType for incoming GIF messages. Odd issues with receiving some GIF messages. Not sure if its a custom client thing or a parsing issue with BeautifulSoup

EDIT: It is a parsing issue. It happens when a GIF or Video is sent in two parts. Here is a debug log sample. Notice the second portion is not considered an addition to the first portion is is just considered a "document" by the api because there is no type data.

2023-10-18 17:24:55 DEBUG: [^] [Kik Connection] Received raw data: b'<message to="lucky_bot_69_yx3@talk.kik.com" type="groupchat" xmlns="jabber:client" from="be76ygcvjr2jiydljwvsoqnmmpzi37b4et25hfi5pv6soveqvfpq_a@talk.kik.com" id="caf8030a-1776-47e7-bbf6-a0ff7f43d5ab" cts="1697664294288"><kik qos="true" timestamp="1697664294276" hop="true" push="true" app="chat"/><pb/><content v="2" app-id="com.kik.ext.gif" id="9201d717-b8e0-4d5d-9492-dc826acc85a2"><strings><app-name>GIF</app-name><layout>video</layout><allow-forward>true</allow-forward><disallow-save>true</disallow-save><video-should-autoplay>true</video-should-autoplay><video-should-loop>true</video-should-loop><video-should-be-muted>true</video-should-be-muted></strings><images><icon>iVBOR(...)==</icon><preview>/9j/4A(...)fs5X3r'
2023-10-18 17:24:55 DEBUG: [^] [Kik Connection] Received raw data: b'mpCZJAP(...)/2Q==</preview></images><uris><uri priority="0" file-content-type="video/mp4" type="video">https://media.tenor.com/wH9jFSjtfl0AAAPo/sharon-den-adel-whistling.mp4</uri><uri priority="1" file-content-type="video/webm" type="video">https://media.tenor.com/wH9jFSjtfl0AAAPs/sharon-den-adel-whistling.webm</uri><uri priority="0" file-content-type="video/tinymp4" type="video">https://media.tenor.com/wH9jFSjtfl0AAAP1/sharon-den-adel-whistling.mp4</uri><uri priority="1" file-content-type="video/tinywebm" type="video">https://media.tenor.com/wH9jFSjtfl0AAAP3/sharon-den-adel-whistling.webm</uri><uri priority="0" file-content-type="video/nanomp4" type="video">https://media.tenor.com/wH9jFSjtfl0AAAP2/sharon-den-adel-whistling.mp4</uri><uri priority="1" file-content-type="video/nanowebm" type="video">https://media.tenor.com/wH9jFSjtfl0AAAP4/sharon-den-adel-whistling.webm</uri></uris></content><request xmlns="kik:message:receipt" d="true" r="true"/><g jid="1100251194664_g@groups.kik.com"/><alias-sender>be76ygcvjr2jiydljwvsoqnmmpzi37b4et25hfi5pv6soveqvfpq_a@talk.kik.com</alias-sender></message>'
Exception in thread Thread-661 (_on_new_data_received):
Traceback (most recent call last):
  File "/usr/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ian/PycharmProjects/lucky_bot/venv/lib/python3.11/site-packages/kik_unofficial/client.py", line 621, in _on_new_data_received
2023-10-18 17:24:55 WARNING: [!] [Thread-662 (_on_new_data_received)] Unknown element type: [document]
    self._handle_xmpp_message(xml_element)
  File "/home/ian/PycharmProjects/lucky_bot/venv/lib/python3.11/site-packages/kik_unofficial/client.py", line 719, in _handle_xmpp_message
    self._handle_kik_event(xmpp_message)
  File "/home/ian/PycharmProjects/lucky_bot/venv/lib/python3.11/site-packages/kik_unofficial/client.py", line 732, in _handle_kik_event
    xmlns_handlers.GroupXMPPMessageHandler(self.callback, self).handle(xmpp_element)
  File "/home/ian/PycharmProjects/lucky_bot/venv/lib/python3.11/site-packages/kik_unofficial/xmlns_handlers.py", line 155, in handle
    self.callback.on_gif_received(IncomingGifMessage(data))
                                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ian/PycharmProjects/lucky_bot/venv/lib/python3.11/site-packages/kik_unofficial/datatypes/xmpp/chatting.py", line 502, in __init__
    self.uris = [self.Uri(uri) for uri in data.content.uris]
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not iterable
tomer8007 commented 11 months ago

Any more changes you want to commit?

TheGreatCodeholio commented 11 months ago

Nope no more changes for now.

TheGreatCodeholio commented 11 months ago

I lied, I had one more to fix the above multi packet issue.

tomer8007 commented 11 months ago

Your latest changes with the parsing issue look good, I'm waiting for your final commit.

TheGreatCodeholio commented 11 months ago

I am making an experimental commit. That I may revert if it doesn't work as I want it to. I did some more tinkering with received data.

TheGreatCodeholio commented 11 months ago

Changes worked like a charm for data packets received. It was having issues due to data collecting in partial data. With this new way if something doesn't match it sits in buffer. Looking in to ways to remove items from buffer after a timeout so we don't have memory leaks.

tomer8007 commented 11 months ago

I left some comments on your previous commits

tomer8007 commented 10 months ago

Thanks for the fixes and the enhancements, this is better, but I really think that parsing XML with regex is a bad idea for the long term. It's probably worth figuring out how to do this in BeautifulSoup, it should be easy. Don't you think?

bluemods commented 10 months ago

Thanks for the fixes and the enhancements, this is better, but I really think that parsing XML with regex is a bad idea for the long term. It's probably worth figuring out how to do this in BeautifulSoup, it should be easy. Don't you think?

The only reliable way to parse an XML stream (XMPP) that I found is pull parsing, where the input stream is fed directly into the parser. Regex / manual checks for where a tag might be closed is error prone, inefficient, and will eventually break. You can see my implementation in Go here: https://github.com/bluemods/kik-go-proxy/blob/8c434d8d8a27e7d0bef838ba5597e07635f5187a/node.go#L77C12-L77C12

The problem with integrating it into this repo could mean massive refactoring, but it's probably worth it to do.

tomer8007 commented 10 months ago

@TheGreatCodeholio You now have to work with @bluemods new XML parser in #241. Looking forward for the video sending support.