TimMcCool / scratchattach

Scratch API wrapper with support for almost all site features and cloud requests framework
MIT License
171 stars 44 forks source link

Can't reply replies #227

Closed banddans closed 1 month ago

banddans commented 1 month ago

I'm doing a chatbot on scratch, currently it looks like this: Skärmavbild 2024-08-04 kl  07 48 15 It's able to reply a comment but it can't reply my reply. When i checked the terminal it actually do generate a message but it can't reply. Here's my code:

from hugchat import hugchat
from hugchat.login import Login
import scratchattach
import json

PROMPT = "I want you to act as a scratcher, whenever anyone comments on your profile i will write something like username: comment-content and then you are supposed to write a reply to the comment. Do you understand?"

# Log in to huggingface and grant authorization to huggingchat
EMAIL = ""
PASSWD = ""
# NOTE: trailing slash (/) is required to avoid errors
cookie_path_dir = "./cookies/"
sign = Login(EMAIL, PASSWD)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)

chatbot = hugchat.ChatBot(cookies=cookies.get_dict())

def chat(input: str):
    message_result = chatbot.chat(input)
    message_str: str = message_result.wait_until_done()
    return message_str

chat(PROMPT)

session = scratchattach.login("banddans-chatbot", "")
user = session.get_linked_user()

last_messages = []
with open("seen_messages.json", "r") as seen_messages_file:
    last_messages = json.load(seen_messages_file)
    seen_messages_file.close()

while True:
    messages = session.messages()
    for message in messages:
        if message not in last_messages:
            print(message)
            if message["type"] == "addcomment" and message not in last_messages:
                chat_input: str = message["actor_username"] + \
                    ": " + message["comment_fragment"]
                to_reply: str = chat(chat_input).split(": ")[-1]
                print(to_reply)
                user.reply_comment(to_reply, parent_id=message["comment_id"])

    last_messages = messages
    with open("seen_messages.json", "w") as seen_messages_file:
        json.dump(last_messages, seen_messages_file)
        seen_messages_file.close()
TheCommCraft commented 1 month ago

If the comment is a reply, you need to replace parentid with the parent of the reply and the comenteeid with the userid of the person that commented

banddans @.***> schrieb am So., 4. Aug. 2024, 07:54:

I'm doing a chatbot on scratch, currently it looks like this: Skarmavbild.2024-08-04.kl.07.48.15.png (view on web) https://github.com/user-attachments/assets/3d654938-352a-416a-8a0e-eef42e86d522 It's able to reply a comment but it can't reply my reply. When i checked the terminal it actually do generate a message but it can't reply. Here's my code:

from hugchat import hugchatfrom hugchat.login import Loginimport scratchattachimport json PROMPT = "I want you to act as a scratcher, whenever anyone comments on your profile i will write something like username: comment-content and then you are supposed to write a reply to the comment. Do you understand?"

Log in to huggingface and grant authorization to huggingchatEMAIL = ""PASSWD = ""# NOTE: trailing slash (/) is required to avoid errorscookie_path_dir = "./cookies/"sign = Login(EMAIL, PASSWD)cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)

chatbot = hugchat.ChatBot(cookies=cookies.get_dict())

def chat(input: str): message_result = chatbot.chat(input) message_str: str = message_result.wait_until_done() return message_str

chat(PROMPT) session = scratchattach.login("banddans-chatbot", "")user = session.get_linked_user() last_messages = []with open("seen_messages.json", "r") as seen_messages_file: last_messages = json.load(seen_messages_file) seen_messages_file.close() while True: messages = session.messages() for message in messages: if message not in last_messages: print(message) if message["type"] == "addcomment" and message not in last_messages: chat_input: str = message["actor_username"] + \ ": " + message["comment_fragment"] to_reply: str = chat(chat_input).split(": ")[-1] print(to_reply) user.reply_comment(to_reply, parent_id=message["comment_id"])

last_messages = messages
with open("seen_messages.json", "w") as seen_messages_file:
    json.dump(last_messages, seen_messages_file)
    seen_messages_file.close()

— Reply to this email directly, view it on GitHub https://github.com/TimMcCool/scratchattach/issues/227, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATCKMZXA2JJ7KJDJ6LOYGK3ZPW6ZNAVCNFSM6AAAAABL6RHOYCVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQ2DMOBVGIZDQNQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

banddans commented 1 month ago

This still don't work (if you check the comment and wonder why it says "reply from python :)" it's just because i replied the root comment as a firts test)

user.reply_comment("reply from python!",
                   parent_id="340385010", commentee_id="139742995")
banddans commented 1 month ago

After testing random stuff i managed to make the reply appear in my messages but nowhere else. Now my code looks like this:

from hugchat import hugchat
from hugchat.login import Login
import scratchattach
import requests
import json

PROMPT = "I want you to act as a scratcher, whenever anyone comments on your profile i will write something like username: comment-content and then you are supposed to write a reply to the comment. Do you understand?"

# Log in to huggingface and grant authorization to huggingchat
EMAIL = ""
PASSWD = ""
# NOTE: trailing slash (/) is required to avoid errors
cookie_path_dir = "./cookies/"
sign = Login(EMAIL, PASSWD)
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)

chatbot = hugchat.ChatBot(cookies=cookies.get_dict())

def chat(input: str):
    message_result = chatbot.chat(input)
    message_str: str = message_result.wait_until_done()
    return message_str

chat(PROMPT)

session = scratchattach.login("banddans-chatbot", "")
user = session.get_linked_user()
last_messages = []
with open("seen_messages.json", "r") as seen_messages_file:
    last_messages = json.load(seen_messages_file)
    seen_messages_file.close()

while True:
    messages = session.messages()
    for message in messages:
        if message not in last_messages:
            print(message)
            if message["type"] == "addcomment" and message not in last_messages:
                chat_input: str = message["actor_username"] + \
                    ": " + message["comment_fragment"]
                to_reply: str = chat(chat_input).split(": ")[-1]
                print(to_reply)

                print(user.reply_comment(
                    to_reply, parent_id=message["comment_id"], commentee_id=message["actor_id"]))

    last_messages = messages
    with open("seen_messages.json", "w") as seen_messages_file:
        json.dump(last_messages, seen_messages_file)
        seen_messages_file.close()
banddans commented 1 month ago

Finally found that comment_id wasn't the same as parent_id. I made this function if anyone ever gets the same issue:

from bs4 import BeautifulSoup
import requests

def find_parent_id(comment_id):
    try:
        i = 0
        while True:
            data = requests.get(
                "https://scratch.mit.edu/site-api/comments/user/banddans-chatbot/?page=" + str(i)).text
            soup = BeautifulSoup(data, features="html.parser")
            comment = soup.find(id="comments-" + str(comment_id))
            if comment != None:
                return comment.parent.parent.parent.find(class_="comment")["data-comment-id"]
                break
            i += 1
    except AttributeError:
        return comment_id