Gammu SMSD

安装gammu配置网卡

apt-get install gammu

查看usb 网卡modem

ls /dev/ttyUSB*

大部分网卡使用的是最后一个,如 ls /dev/ttyUSB*,看刀最大数值为2,则使用ttyUSB2

# ls /dev/ttyUSB*
/dev/ttyUSB0  /dev/ttyUSB1  /dev/ttyUSB2

安装完成运行gammu-config配置,配置文件会保存在~/.gammurc

gammu-config

port设置为/dev/ttyUSB2,connection为at19200,随后保存。

使用gammu identify 确认port设置是否正确

gammu identify
###如有一下信息,则说明配置正确
Device               : /dev/ttyUSB3
Manufacturer         : ZTE
Model                : unknown (MF110)
Firmware             : BD_UCP671A2V1.0.1B01
IMEI                 : 564226008907948
SIM IMSI             : 160215867486905

使用gammu-smsd接收短信

# apt-get install  gammu-smsd -y

编辑/etc/gammu-smsdrc

# Configuration file for Gammu SMS Daemon

# Gammu library configuration, see gammurc(5)
[gammu]
# Please configure this!
port = /dev/ttyUSB2
connection = at19200
# Debugging
#logformat = textall

# SMSD configuration, see gammu-smsdrc(5)
[smsd]
service = file
logfile = syslog
#RunOnReceive = /root/smsRecv
# Increase for debugging information
debuglevel = 0

# Paths where messages are stored
inboxpath = /var/spool/gammu/inbox/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error/

在其他手机上发短信或收验证码,测试配置是否可用,如果没问题的话,在/var/spool/gammu/inbox可以看刀短信已经收到。

短信转发到telegram bot

自定义脚本,/root/smsRecv

#!/bin/sh
TOKEN="14f83897:AAFUtqrFRdjk2jdl24nfafHjsQNPu85"
CHAT_ID=9929456
URL="https://api.telegram.org/bot$TOKEN/sendMessage"
for i in `seq $SMS_MESSAGES` ; do
      eval " curl -s -X POST $URL -d chat_id=$CHAT_ID -d text=\"\${SMS_${i}_TEXT}\"" 
done

在telegram种找刀@BotFather,创建一个新bot,创建完成后发一条消息给bot,然后打开https://api.telegram.org/bot{TOKEN}/getUpdates (将“{TOKEN}”替换为刚创建完的bot的api token),获取CHAT_ID,随后修改/etc/gammu-smsdrc,将service修改为null,添加RunOnReceive=/root/smsRecv。

重启smsd服务

systemctl restart gammu-smsd

Link:http://wenbinwu.com/%E7%82%B9%E7%82%B9%E6%BB%B4%E6%BB%B4/2018/04/25/raspberry-pi-sms.html

nodejs 转发telegram

安装nodejs和yarn:

nodejs

curl -sL https://deb.nodesource.com/setup_11.x | bash -
apt-get install -y nodejs

yarn

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

新建目录gammu-telegram,新建package.json、index.js:

# cat package.json 
{
  "name": "gammu-telegram",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "axios": "^0.18.0",
    "socks-proxy-agent": "^4.0.1"
  }
}
# cat index.js 
#!/usr/bin/node
const fs = require('fs')
const path = require('path')
const axios = require('axios')
const SocksProxyAgent = require('socks-proxy-agent')

const botToken = "14f83897:AAFUtqrFRdjk2jdl24nfafHjsQNPu85"
const chatId = 9929456

//代理设置

//const proxyOptions = 'socks5://127.0.0.1:1080' 
//const httpsAgent = new SocksProxyAgent(proxyOptions)

//无代理
const client = axios.create({ baseURL: `https://api.telegram.org/bot${botToken}`})
//使用代理
//const client = axios.create({ baseURL: `https://api.telegram.org/bot${botToken}`, httpsAgent})

// fs.writeFileSync(path.resolve(__dirname + '/env'), JSON.stringify(process.env))
const { SMS_1_NUMBER, SMS_1_TEXT, DECODED_0_TEXT } = process.env

let smsData

if (DECODED_0_TEXT) {
  smsData = DECODED_0_TEXT
} else {
  smsData = SMS_1_TEXT
}

async function main() {
  await client.post('/sendMessage', {
    chat_id: chatId,
    text: "Phone Number: `" + SMS_1_NUMBER + "`\n" + "Tag: #sms\nData: `" + smsData + "`",
    parse_mode: 'Markdown'
  })
}

main()
  .catch(err => console.log(err))

然后执行

yarn install 
chmox +x index.js

之后将/etc/gammu-smsdrc 中RunOnReceive修改为/root/gammu-telegram/index.js, 重启smsd。

# cat /etc/gammu-smsdrc 
# Configuration file for Gammu SMS Daemon

# Gammu library configuration, see gammurc(5)
[gammu]
# Please configure this!
port = /dev/ttyUSB2
connection = at19200
# Debugging
logformat = textall

# SMSD configuration, see gammu-smsdrc(5)
[smsd]
service = null
logfile = syslog
#RunOnReceive = /root/smsRecv
RunOnReceive = /root/gammu-telegram/index.js
# Increase for debugging information
debuglevel = 0

# Paths where messages are stored
inboxpath = /var/spool/gammu/inbox/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error/
systemctl restart gammu-smsd

开机启动smsd

systemctl enable gammu-smsd

发表评论

电子邮件地址不会被公开。 必填项已用*标注