在 Telegram 的搜索框中输入 @BotFather,点进去,点击 Start 开始,输入
/newbot
返回以下内容
Alright, a new bot. How are we going to call it? Please choose a name for your bot.
输入聊天机器人的 name,这个名称在整个 Telegram 不是唯一的,可以用中文,如:”接收短信“,点击发送,返回以下内容
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.
输入聊天机器人的 ID,这个 ID 在整个 Telegram 里唯一的,不可以用中文,必须以 bot 结尾,如 SMSBot 或者 SMS_bot。如过返回以下内容,说明名字重复了,再输入一个新的名字就行了,直到不再返回以下的消息为止
Sorry, this username is already taken. Please try something different.
如果返回了以下内容,说明成功创建了聊天机器人
Done! Congratulations on your new bot. You will find it at t.me/sms_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.
Use this token to access the HTTP API:
6046235698:BBCt4ENwTn5F8E73PQDJiOW5UTuCBJs0BpB
Keep your token secure and store it safely, it can be used by anyone to control your bot.
For a description of the Bot API, see this page: https://core.telegram.org/bots/api
其中 HTTP API 就是接下来要用 token,长这样,保存下来
6046235698:BBCt4ENwTn5F8E73PQDJiOW5UTuCBJs0BpB
刚才返回内容里的开头,有个链接,长这样
t.me/sms_bot.
点击它就会弹出机器人的聊天窗口,点击开始,一定要点击开始,聊天机器人才可以给你发信息,返回 Telegram 主界面,搜索 @userinfobot,点击进去,点击开始,它会反回消息
@username
Id: 7831355379
First: Rick
Last: Blaine
Lang: en
其中的 Id 就是之后要用的 chat_id,在终端先用以下命令测试一下
curl -d "chat_id=<chat_id>&text=<text>" -X POST https://api.telegram.org/bot<token>/sendMessage
把
curl -d "chat_id=7831355379&text=hello" -X POST https://api.telegram.org/bot6046235698:BBCt4ENwTn5F8E73PQDJiOW5UTuCBJs0BpB/sendMessage
如果 Telegram 可以接收到到消息,再接着往下。在OpenWRT软件包里安装 iconv ,这个插件用来解决中文乱码问题。输入以下命令,编写把新接收的短信发送到 Telegram 的脚本
vim /root/pushsms.sh
输入以下内容,把下面的 chat_id 和 token 改成自己的
#!/bin/sh
# 把刚接收的短信转换为文本
if [ "$1" == "RECEIVED" ]; then
from=`grep "From:" $2 | awk -F ': ' '{printf $2}'`
#sent=`grep "Sent:" $2 | awk -F ': ' '{printf $2}'`
#received=`grep "Received:" $2 | awk -F ': ' '{printf $2}'`
alphabet=`grep "Alphabet:" $2 | awk -F ': ' '{printf $2}'`
if [ "$alphabet" = "UCS2" ]; then
content=$(sed -e '1,/^$/ d' < "$2" | iconv -f UNICODEBIG -t UTF-8)
else
content=$(sed -e '1,/^$/ d' < "$2")
fi
text=$(cat <<EOF
$from:
$content
EOF
)
# 配置 Telegram,请修改chat_id 和 token 为你自己的 token
chat_id=7831355379
token=6046235698:BBCt4ENwTn5F8E73PQDJiOW5UTuCBJs0BpB
# 把短信发送到 Telegram,请修改chat_id 和 token 为你自己的 token
curl -d "chat_id=$chat_id&text=$text" -X POST https://api.telegram.org/bot"$token"/sendMessage
fi
输入以下内容给 pushsms.sh 授权,这一步很重要,如果没执行一下命令,可能会导致无法发送邮件
chmod +x /root/pushsms.sh
在 OpenWRT 软件包里安装 smstools3,输入以下命令,配置 smstool3
vim /etc/smsd.conf
填写以下内容
#
# Description: Main configuration file for the smsd
#
devices = GSM1
incoming = /var/spool/sms/incoming
outgoing = /var/spool/sms/outgoing
checked = /var/spool/sms/checked
failed = /var/spool/sms/failed
sent = /var/spool/sms/sent
receive_before_send = no
autosplit = 3
# 设置接收短信的编码格式为 utf8,不然会出现乱码
incoming_utf8 = yes
loglevel = notice
#delaytime = 0
# 这里链接刚才编写的脚本,意思是如果有新的短信就触发该脚本
eventhandler = /root/pushsms.sh
[GSM1]
# ME模式(将短信保存在上网卡中,更快,不支持的话请使用下面的 SM模式)
init = AT+CPMS="ME","ME","ME"
# SM模式(将短信保存在 USIM 卡中)
# init = AT+CPMS="SM","SM","SM"
# 这里要修改成你对应的端口号
device = /dev/ttyUSB2
incoming = yes
#pin = 0000
# 波特率一般都是115200,如果不对修改成你自己的
baudrate = 115200
signal_quality_ber_ignore = yes
detect_unexpected_input = no
memory_start = 0
重启 smsd 进程
/etc/init.d/smstools3 restart
最后用自己的手机给软路由的手机号发送短信,就可以在 Telegram 里收到短信了。
参考文献
[1] 设置 Telegram 接受消息 API.
[2] 把接收的短信转换为文本.
[3] 解决乱码问题