Add stdinput detection, remove commandline flag
This commit is contained in:
parent
232d43310c
commit
3ea653fc6f
@ -26,7 +26,6 @@ def handle():
|
|||||||
messaging.add_argument("--me", action="store_true", help="Send the message to yourself")
|
messaging.add_argument("--me", action="store_true", help="Send the message to yourself")
|
||||||
messaging.add_argument("--target", "-t", metavar="chatid", type=int, help="Send the message to the given chat")
|
messaging.add_argument("--target", "-t", metavar="chatid", type=int, help="Send the message to the given chat")
|
||||||
messaging.add_argument("--message", "--msg", "-m", help="Provide a message.")
|
messaging.add_argument("--message", "--msg", "-m", help="Provide a message.")
|
||||||
messaging.add_argument("--stdin", "-i", action="store_true", help="Read a message from stdin.")
|
|
||||||
parsed = parser.parse_args()
|
parsed = parser.parse_args()
|
||||||
global debug
|
global debug
|
||||||
if parsed.verbose:
|
if parsed.verbose:
|
||||||
@ -58,6 +57,8 @@ def handle():
|
|||||||
debug("Fetching chats...", file=sys.stderr)
|
debug("Fetching chats...", file=sys.stderr)
|
||||||
chats = client.get_dialogs()
|
chats = client.get_dialogs()
|
||||||
|
|
||||||
|
stdinput = "".join([line for line in sys.stdin])
|
||||||
|
|
||||||
filtered = chats if parsed.list else filter_chats((parsed.startswith, parsed.contains, parsed.matches))
|
filtered = chats if parsed.list else filter_chats((parsed.startswith, parsed.contains, parsed.matches))
|
||||||
unique = None
|
unique = None
|
||||||
if filtered:
|
if filtered:
|
||||||
@ -67,12 +68,12 @@ def handle():
|
|||||||
for result in reversed(filtered):
|
for result in reversed(filtered):
|
||||||
print(str(result.id).rjust(16) + " "*4 + result.name)
|
print(str(result.id).rjust(16) + " "*4 + result.name)
|
||||||
else:
|
else:
|
||||||
if not (parsed.message or parsed.stdin):
|
if not (parsed.message or stdinput):
|
||||||
for result in reversed(filtered):
|
for result in reversed(filtered):
|
||||||
print(str(result.id).rjust(16) + " "*4 + result.name)
|
print(str(result.id).rjust(16) + " "*4 + result.name)
|
||||||
exit()
|
exit()
|
||||||
unique = filtered[0].id
|
unique = filtered[0].id
|
||||||
if not (parsed.message or parsed.stdin):
|
if not (parsed.message or stdinput):
|
||||||
exit() # we are done here
|
exit() # we are done here
|
||||||
|
|
||||||
recipient = unique or parsed.target
|
recipient = unique or parsed.target
|
||||||
@ -92,11 +93,11 @@ def handle():
|
|||||||
# print("Illegal entity id. Aborting.", file=sys.stderr)
|
# print("Illegal entity id. Aborting.", file=sys.stderr)
|
||||||
# exit(1)
|
# exit(1)
|
||||||
|
|
||||||
if parsed.message or parsed.stdin:
|
#if parsed.message or parsed.stdin:
|
||||||
send_message(client, recipient, message=parsed.message)
|
send_message(client, recipient, message=parsed.message, stdinput=stdinput)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def send_message(client, chat_id, message):
|
def send_message(client, chat_id, message, stdinput):
|
||||||
#print(f"call to {client} {chat_id} {message}")
|
#print(f"call to {client} {chat_id} {message}")
|
||||||
try:
|
try:
|
||||||
recipient = client.get_input_entity(chat_id)
|
recipient = client.get_input_entity(chat_id)
|
||||||
@ -104,13 +105,14 @@ def send_message(client, chat_id, message):
|
|||||||
print("Could not find the entity for this entity id. Aborting.", file=sys.stderr)
|
print("Could not find the entity for this entity id. Aborting.", file=sys.stderr)
|
||||||
exit(1)
|
exit(1)
|
||||||
debug("Chat exists. Sending message.", file=sys.stderr)
|
debug("Chat exists. Sending message.", file=sys.stderr)
|
||||||
if message is None:
|
if message and stdinput:
|
||||||
debug("No message specified. Reading from stdin.", file=sys.stderr)
|
out = f"{message}\n{stdinput}"
|
||||||
message = "".join([line for line in sys.stdin])
|
else:
|
||||||
if message.strip() == "":
|
out = message or stdinput
|
||||||
|
if not out.strip():
|
||||||
print("The message must not be empty.", file=sys.stderr)
|
print("The message must not be empty.", file=sys.stderr)
|
||||||
exit()
|
exit()
|
||||||
client.send_message(chat_id, message)
|
client.send_message(chat_id, out)
|
||||||
|
|
||||||
def filter_chats(filt):
|
def filter_chats(filt):
|
||||||
if filt == (None, None, None):
|
if filt == (None, None, None):
|
||||||
|
Loading…
Reference in New Issue
Block a user