bash Telegram-cli:脚本不发送消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29531891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Telegram-cli : Script not sending message
提问by raspayu
I am making a simple bash script with Telegram-cli, in which I send 2 parameters (destination and message), and it should send a Telegram message to the destination (Name_FamilyName).
我正在使用 Telegram-cli 制作一个简单的 bash 脚本,在其中发送 2 个参数(目的地和消息),它应该向目的地(Name_FamilyName)发送一条 Telegram 消息。
The script looks as follows:
该脚本如下所示:
#!/bin/bash
destination=;
message=;
(echo "msg $destination $message"; echo "safe_quit") | bin/telegram-cli -k tg-server.pub -W
With that, in theory, messages should be sent. I change the permissions of the script, and I call it in the next way:
有了这个,理论上,应该发送消息。我更改了脚本的权限,并以以下方式调用它:
./script_send_message.sh Max_Musterman "Hola qute tal estas"
And that is the output I get:
这就是我得到的输出:
Telegram-cli version 1.2.0, Copyright (C) 2013-2015 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
Telegram-cli uses libtgl version 1.2.0
I: config dir=[/home/machine/.telegram-cli]
> msg Max_Musterman Hola qute tal estas
> safe_quit
User Max_Musterman updated username
User Max_Musterman online (was online [2015/04/09 06:56:04])
User Test Phone offline (was online [2015/04/09 06:51:42])
> > All done. Exit
halt
No message has been sent at all. Insted, if I send exactly the same message from the console, it works fine. Here is what I do:
根本没有发送任何消息。Insted,如果我从控制台发送完全相同的消息,它工作正常。这是我所做的:
bin/telegram-cli -k server.pub -W
Telegram-cli version 1.2.0, Copyright (C) 2013-2015 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type `show_license'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show_license' for details.
Telegram-cli uses libtgl version 1.2.0
I: config dir=[/home/machine/.telegram-cli]
User Max_Musterman updated username
User Max_Musterman online (was online [2015/04/09 06:59:46])
User Max_Musterman offline (was online [2015/04/09 06:51:42])
> msg Max_Musterman Hola qute tal estas
[06:57] Max_Musterman <<< Hola qute tal estas
User Max_Musterman marked read 1 outbox and 0 inbox messages
User Max_Musterman offline (was online [2015/04/09 06:57:29])
>
I am running out of ideas. I start to think that, somehow, the contact list is not loaded when the command msg is sent in the script, so it sends nothing(in the console, if you send a message to a made up user, it won't do anything like in the script).
我的想法不多了。我开始认为,不知何故,在脚本中发送命令 msg 时没有加载联系人列表,所以它什么都不发送(在控制台中,如果你向一个虚构的用户发送消息,它不会做任何事情就像在脚本中一样)。
Has anyone experienced something similar? Any solution? Thank you for your help.
有没有人经历过类似的事情?有什么解决办法吗?感谢您的帮助。
回答by xabi
You have other option:
您还有其他选择:
instead of piping commands to telegram-cli you can use "-e" option and the user_id, this way:
您可以使用“-e”选项和 user_id,而不是将命令传递给 Telegram-cli,如下所示:
telegram-cli -RD -e "msg user#nnnnnnn Hola caracola"
where nnnnn is the user_id. You can find it via the "user_info ....." command.
其中 nnnnn 是 user_id。您可以通过“user_info .....”命令找到它。
This way you don't need to sleep, just make the telegram-cli do all the work, and without using the -W command telegram doesn't need to get all your contacts.
这样您就不需要睡觉,只需让电报-cli 完成所有工作,并且不使用 -W 命令电报不需要获取所有联系人。
回答by raspayu
Looks like when you run telegram_cli from the script, it needs a bit of time to be able to send any message (until it shows the list of Users at least). If you send a message before the user list is loaded, you won't be able to send anything. So a quick fix (or we may call it a naughty hack) is to tell the script to wait 3 seconds for sending the message:
看起来当您从脚本运行 telegram_cli 时,它需要一些时间才能发送任何消息(直到它至少显示用户列表)。如果您在加载用户列表之前发送消息,您将无法发送任何内容。所以一个快速的解决方法(或者我们可以称之为顽皮的黑客)是告诉脚本等待 3 秒来发送消息:
#!/bin/bash
destination=;
message=;
(sleep 3;echo "msg $destination $message"; echo "safe_quit") | bin/telegram-cli -k tg-server.pub -W
Maybe you have to change the sleep3 to sleep 5 or something like that, but it should be able to send the messages after that.
也许您必须将 sleep3 更改为 sleep 5 或类似的内容,但它应该能够在此之后发送消息。