1
|
|
|
#!/usr/bin/env php |
2
|
|
|
<?php |
3
|
|
|
|
4
|
|
|
declare(strict_types=1); |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
8
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace OCA\TwoFactorGateway\Provider\Channel\Telegram\Provider\Drivers\ClientCli; |
12
|
|
|
|
13
|
|
|
use danog\MadelineProto\API; |
14
|
|
|
use danog\MadelineProto\RPCErrorException; |
15
|
|
|
use danog\MadelineProto\Settings; |
16
|
|
|
use danog\MadelineProto\Settings\Logger; |
17
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
18
|
|
|
use Symfony\Component\Console\Command\Command; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
#[AsCommand(name: 'telegram:send-message', description: 'Send a message via Telegram Client API')] |
24
|
|
|
class SendMessage extends Command { |
25
|
|
|
|
26
|
|
|
protected function configure(): void { |
27
|
|
|
$this |
28
|
|
|
->addOption( |
29
|
|
|
'session-directory', |
30
|
|
|
's', |
31
|
|
|
InputOption::VALUE_REQUIRED, |
32
|
|
|
'Directory to store the session files', |
33
|
|
|
) |
34
|
|
|
->addOption( |
35
|
|
|
'to', |
36
|
|
|
't', |
37
|
|
|
InputOption::VALUE_REQUIRED, |
38
|
|
|
'Recipient (username or user ID)', |
39
|
|
|
) |
40
|
|
|
->addOption( |
41
|
|
|
'message', |
42
|
|
|
'm', |
43
|
|
|
InputOption::VALUE_REQUIRED, |
44
|
|
|
'Message to send', |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int { |
49
|
|
|
$sessionDirectory = $input->getOption('session-directory'); |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
$settings = (new Settings()) |
53
|
|
|
->setLogger((new Logger())->setExtra($sessionDirectory . '/MadelineProto.log')); |
54
|
|
|
|
55
|
|
|
$message = $input->getOption('message'); |
56
|
|
|
$peer = $input->getOption('to'); |
57
|
|
|
|
58
|
|
|
$api = new API($sessionDirectory, $settings); |
59
|
|
|
|
60
|
|
|
$api->start(); |
61
|
|
|
|
62
|
|
|
if (!str_starts_with($peer, '@')) { |
63
|
|
|
try { |
64
|
|
|
$result = $api->contacts->resolvePhone(phone: $peer); |
65
|
|
|
$peer = $result['peer']; |
66
|
|
|
} catch (RPCErrorException $e) { |
67
|
|
|
if ($e->getMessage() === 'PHONE_NOT_OCCUPIED') { |
68
|
|
|
$output->writeln('<error>Error: The phone number is not associated with any Telegram account.</error>'); |
69
|
|
|
return Command::FAILURE; |
70
|
|
|
} |
71
|
|
|
throw $e; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$api->messages->sendMessage([ |
77
|
|
|
'peer' => $peer, |
78
|
|
|
'message' => $message, |
79
|
|
|
'parse_mode' => 'Markdown', |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
$output->writeln('<info>Message sent successfully!</info>'); |
83
|
|
|
|
84
|
|
|
return Command::SUCCESS; |
85
|
|
|
} catch (\Throwable $e) { |
86
|
|
|
$output->writeln('<error>Error: ' . $e->getMessage() . '</error>'); |
87
|
|
|
return Command::FAILURE; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|