3

I have a task to make changes to the message and forward it to other channels. By changes I mean replace some x strings to y both in message text and buttons. So I created something like below:

@client.on(events.NewMessage(chats=[channelId]))
async def handler(event):
    channels = get_channels()
    replacements = get_replacements()
    msg_first = copy.copy(event.message.text)
    btn_first = copy.deepcopy(event.message.buttons)
    for channel in channels:
        replacements_api = get_replacement(channel['channel_id'], replacements)
        message_text = msg_first
        message_buttons = btn_first
        for rep_api in replacements_api:
            message_text = message_text.replace(rep_api['word'], rep_api['word_replacement'])

            if message_buttons:
                for button in message_buttons:
                    text = button[0].button.text
                    url = button[0].button.url
                    button[0].button.text = text.replace(rep_api['word'], rep_api['word_replacement'])
                    button[0].button.url = url.replace(rep_api['word'], rep_api['word_replacement'])

        event.message.text = message_text
        await client.send_message(channel['channel_id'], message=event.message, buttons=message_buttons)

But here I got an error:

 File "/usr/lib/python3.9/copy.py", line 230, in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)
  File "/usr/lib/python3.9/copy.py", line 161, in deepcopy
    rv = reductor(4)
TypeError: cannot pickle 'sqlite3.Connection' object
1
  • 3
    Show the full stack trace. Commented Aug 1, 2022 at 10:37

1 Answer 1

4
+25

An SQLite connection is a stateful object synchronized with your system via the IOs it performed. By SQLite's library design, it will not be able to support the duplication and the sharing of the underlying handles of system resources.

The impossible copy might have been triggered by your call btn_first = copy.deepcopy(event.message.buttons). Make sure these buttons only contain copyable data, or consider making your client send something simpler.

Note that if they are actual interactive buttons, they are attached to a system resource (a handle to the window system) and local callbacks, and therefore are not deeply copyable either.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.