Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add text styling support #42

Merged
merged 5 commits into from
Jan 13, 2024
Merged

add text styling support #42

merged 5 commits into from
Jan 13, 2024

Conversation

mariopaolo
Copy link
Contributor

@mariopaolo mariopaolo commented Dec 28, 2023

Add support for Signal text styling.

In Signal the only way to apply text styling is through the UI, but https://github.com/AsamK/signal-cli supports both reading and setting text styles.
Example from signal-cli logs:

..., "textStyles":[{"style":"ITALIC", "start":0, "length":5}], ...

As you can see also from the text parser in https://github.com/bbernhard/signal-cli-rest-api, text styling through API is realized using familiar markdown:

  • **word** for bold
  • *word* for italic
  • ~word~ for strikethrough
  • ||word|| for ||spoiler||
  • `word` for monospaced text

So the styling is done directly in the text using a very small subset of markdown syntax, and it's only necessary to set text_mode to styled in the payload to enable it.

Since parsing is handled upstream, in signalbot I only introduced support for specifying text_mode="styled" in the payload when sending messages.

I tested it with a modified minimal example:

import os
from signalbot import SignalBot, Command, Context

class PingCommand(Command):
    async def handle(self, c: Context):
        if c.message.text == "Ping":
            await c.send("**Pong**", text_mode="styled")
            await c.send("*Pong*", text_mode="styled")
            await c.send("~Pong~", text_mode="styled")
            await c.send("||Pong||", text_mode="styled")
            await c.send("`Pong`", text_mode="styled")

if __name__ == "__main__":
    bot = SignalBot({
        "signal_service": os.environ["SIGNAL_SERVICE"],
        "phone_number": os.environ["PHONE_NUMBER"]
    })
    bot.register(PingCommand()) # all contacts and groups
    bot.start()

If text_mode is not set, it defaults to normal (so without styling).

@filipre
Copy link
Owner

filipre commented Dec 28, 2023

/deploy

Copy link

Pull Request has been deployed on test.pypi.org: 0.8.0a1703799240 🚀 You can test the package by running

pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple signalbot==0.8.0a1703799240

@filipre
Copy link
Owner

filipre commented Dec 28, 2023

Thanks a lot for the PR! Very easy solution. I wasn't aware that the HTTP wrapper supports formatting. It will be available in the next release. Until then, you can use the test version from above.

There is only one thing. I was planning to refactor the project to be independent of the backend (signal-http-wrapper, signald) but it looks like the formatting is only available in the http wrapper. Though, I will deal with that then later (if ever).

@filipre
Copy link
Owner

filipre commented Dec 28, 2023

Maybe another thing. Can you add your format example in the https://github.com/filipre/signalbot/tree/master/example/commands folder? Right now, documentation is not there yet and there aren't types either, so at least we have the styled keyword at some place.

@mariopaolo
Copy link
Contributor Author

mariopaolo commented Dec 28, 2023

@filipre sure, I'll add the example. is it ok if I create a styledping.py file inside example/commands?
any other name is good to me, maybe styling.py or textstyles.py

there is only one thing that is puzzling me and that is the spoiler tag.
it actually doesn't work (I removed it from the PR description) while it's one of the supported styles and it's even mentioned in signal-cli-rest-api parser, where it's represented by the | character.
But in their API docs this is what they say about text_mode:

Set the text_mode to 'styled' in case you want to add formatting to your text message. Styling Options: italic text, bold text, ~strikethrough text~.

No mention of spoiler supported here.
And in in the footnotes of this page it says this about the spoiler tag:

Implementation may not be complete yet [forum:t/612/196](https://community.signalusers.org/t/612/196)

So not sure what you want to do while one of the options is not working.

UPDATE: it doesn't work in signal-cli-rest-api in the first place, so upstream. I tried to send some queries dieectly to the backend using Postman and it doesn't format spoiler correctly, I get just normal text.

@mariopaolo
Copy link
Contributor Author

@filipre done, there's always time to change it.
let me know if you want it in a different way.

@mariopaolo
Copy link
Contributor Author

mariopaolo commented Dec 29, 2023

after further research, I confirmed that the spoiler style indeed works in the base signal-cli library (the one signal-cli-rest-api is based on).

.\signal-cli.bat send -m "spoiler spoiler" --note-to-self --text-style 0:7:SPOILER

and I get only the first 7 characters with spoilers on:
Screenshot of a Signal message showing the first 7 characters with spoilers correctly applied.

From signal-cli send --help:

  --text-style [TEXT_STYLE [TEXT_STYLE ...]]
                         Style parts of  the  message  text (syntax: start:
                         length:STYLE)

So, it looks like the spoilers style is breaking in signal-cli-rest-api.

Since the application of these styles is completely transparent to signalbot and happens in other libraries, this pull request completes the feature nonetheless.

@mariopaolo
Copy link
Contributor Author

mariopaolo commented Dec 29, 2023

ok nevermind, it works in signal-cli-rest-api (and therefore in signalbot as well) but with two pipes || before and after.
I'll update the example and the pull request description.

@mariopaolo
Copy link
Contributor Author

Pull Request has been deployed on test.pypi.org: 0.8.0a1703799240 🚀 You can test the package by running

pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple signalbot==0.8.0a1703799240

@filipre I just tried to install this package using the exact command posted by the bot, but it seems like this version doesn't include my commits. I just tried in a fresh Docker container (python:latest), run the above command and checked api.py inside /usr/local/lib/python3.12/site-packages/signalbot: the code doesn't contain any of the modifications I introduced.

@filipre
Copy link
Owner

filipre commented Jan 5, 2024

/deploy

Copy link

github-actions bot commented Jan 5, 2024

Pull Request has been deployed on test.pypi.org: 0.8.0a1704439123 🚀 You can test the package by running

pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple signalbot==0.8.0a1704439123

@filipre
Copy link
Owner

filipre commented Jan 5, 2024

I was a bit busy, but will look at it on the weekend

@filipre filipre merged commit 0b6b416 into filipre:master Jan 13, 2024
1 of 2 checks passed
@filipre
Copy link
Owner

filipre commented Jan 13, 2024

Merged, let me know if the new version causes issues. Thanks again for your contribution!

@mariopaolo
Copy link
Contributor Author

just deployed my app with the new version, everything's working as expected! thanks again 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants