一个允许通过signald守护进程使用Signal IM服务进行通信的库。
项目描述
pysignald
pysignald是signald项目的Python客户端,而signald是一个为Signal消息服务提供的命令行客户端。
pysignald允许您以编程方式发送和接收Signal消息。
注意:遗憾的是,这个库可能有些过时,或者其中的一部分可能无法工作,因为上游API不断变化,导致兼容性问题。如果您注意到有任何问题,修复它的MR将受到欢迎。
安装
您可以使用pip安装pysignald
$ pip install pysignald
运行
只需确保您已安装signald。以下是如何使用pysignald的示例
from signald import Signal, Reaction
s = Signal("+1234567890")
# If you haven't registered/verified signald, do that first:
s.register(voice=False)
s.verify("sms code")
# If you want to set your display name, mobilecoin payments address (if using payments), or avatar, you can call set_profile:
s.set_profile(
display_name="My user name",
mobilecoin_address="...", # Base64-encoded PublicAddress, see https://github.com/mobilecoinfoundation/mobilecoin/blob/master/api/proto/external.proto
avatar_filename="/signald-data/avatar.png", # Must be accessible by signald
)
s.send(recipient="+1098765432", text="Hello there!")
s.send(recipient_group_id="YXNkZkFTREZhc2RmQVNERg==", text="Hello group!")
# Get the profile information of someone
profile_info = s.get_profile(recipient="+1098765432")
print(profile_info)
for message in s.receive_messages():
print(message)
s.react(Reaction("🥳", message.source, message.timestamp), message.source["number"])
# Send a read receipt notification which shows the message read checkmark on the receipient side
s.send_read_receipt(recipient=message.source["number"], timestamps=[message.timestamp])
# Echo the message back.
s.send(recipient=message.source["number"], text=message.text)
您还可以使用聊天装饰器接口
from signald import Signal
s = Signal("+1234567890")
@s.chat_handler("hello there", order=10) # This is case-insensitive.
def hello_there(message, match):
# Returning `False` as the first argument will cause matching to continue
# after this handler runs.
stop = False
reply = "Hello there!"
return stop, reply
# Matching is case-insensitive. The `order` argument signifies when
# the handler will try to match (default is 100), and functions get sorted
# by order of declaration secondly.
@s.chat_handler("hello", order=10)
def hello(message, match):
# This will match on "hello there" as well because of the "stop" return code in
# the function above. Both replies will be sent.
return "Hello!"
@s.chat_handler("wave", order=20)
def react_with_waving_hand(message, match):
# This will only react to the received message.
# But it would be possible to send a reply and a reaction at the same time.
stop = True
reply = None
reaction = "👋"
return stop, reply, reaction
@s.chat_handler(re.compile("my name is (.*)")) # This is case-sensitive.
def name(message, match):
return "Hello %s." % match.group(1)
@s.chat_handler("")
def catch_all(message, match):
# This will only be sent if nothing else matches, because matching
# stops by default on the first function that matches.
return "I don't know what you said."
s.run_chat()
身份处理
from signald import Signal
from signald.types import TrustLevel
s = Signal("+1234567890")
# Revoke trust for all identities of a given number
for identity in s.get_identities("+1234001100"):
s.trust(
"+1234001100",
identity.safety_number,
TrustLevel.UNTRUSTED,
)
# Generate QR code data for identity validation
ids = s.get_identities("+1234001177")
ids.sort(key=lambda x: x.added, reverse=True)
# prints base64 encoded validation code of the latest identity of the given number
print(ids[0].qr_code_data)
您可以将ids[0].qr_code_data
的内容通过管道传输到| base64 -D | qrencode -t ansi
以通过Signal应用的二维码扫描器验证身份。
群组信息
from signald import Signal
s = Signal("+1234567890")
# list all groups and members
for group in s.list_groups():
print(group.title)
for member in group.members:
print(member.get("uuid"))
各种
pysignald也支持不同的套接字路径
s = Signal("+1234567890", socket_path="/var/some/other/socket.sock")
如果您运行代理,它还支持TCP套接字。例如,您可以使用socat通过TCP代理signald的UNIX套接字
$ socat -d -d TCP4-LISTEN:15432,fork UNIX-CONNECT:/var/run/signald/signald.sock
然后在pysignald中
s = Signal("+1234567890", socket_path=("your.serveri.ip", 15432))
项目详情
下载文件
下载适合您平台的应用程序文件。如果您不确定选择哪个,请了解更多有关安装软件包的信息。
源代码分布
pysignald-0.1.1.tar.gz (10.8 kB 查看哈希值)
构建分布
pysignald-0.1.1-py3-none-any.whl (10.2 kB 查看哈希值)