Bridge bot replies back to SMS
This commit is contained in:
parent
552f8c3c21
commit
dd6f476a3f
65
main.py
65
main.py
|
@ -15,6 +15,7 @@ import settings
|
||||||
bot = TelegramClient('data/bot', settings.API_ID, settings.API_HASH).start(bot_token=settings.API_TOKEN)
|
bot = TelegramClient('data/bot', settings.API_ID, settings.API_HASH).start(bot_token=settings.API_TOKEN)
|
||||||
|
|
||||||
ESCAPE_CHAR = '\x1d'
|
ESCAPE_CHAR = '\x1d'
|
||||||
|
CTRL_Z = '\x1a'
|
||||||
|
|
||||||
@bot.on(events.NewMessage(pattern='/start'))
|
@bot.on(events.NewMessage(pattern='/start'))
|
||||||
async def start(event):
|
async def start(event):
|
||||||
|
@ -48,6 +49,38 @@ async def delete_message(mid):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def send_message(number, text):
|
||||||
|
logging.info('Sending to %s message: %s...', number, text)
|
||||||
|
|
||||||
|
with Telnet('192.168.168.1', 23, timeout=10) as tn:
|
||||||
|
tn.read_until(b'login: ')
|
||||||
|
|
||||||
|
tn.write(b'admin\n')
|
||||||
|
tn.read_until(b'Password: ')
|
||||||
|
|
||||||
|
tn.write(settings.ROUTER_PASS.encode() + b'\n')
|
||||||
|
tn.read_until(b'UserDevice> ')
|
||||||
|
|
||||||
|
command = 'AT+CMGS={}\r\n'.format(str(number))
|
||||||
|
|
||||||
|
tn.write(command.encode())
|
||||||
|
res = tn.read_until(b'whatever', timeout=4).decode()
|
||||||
|
logging.debug('Response data: %s', res)
|
||||||
|
|
||||||
|
message = text + CTRL_Z
|
||||||
|
|
||||||
|
tn.write(message.encode())
|
||||||
|
res = tn.read_until(b'UserDevice> ', timeout=6).decode()
|
||||||
|
logging.debug('Response data: %s', res)
|
||||||
|
|
||||||
|
if 'OK' in res:
|
||||||
|
logging.info('Successfully sent.')
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
logging.info('Problem sending: %s', res)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
async def get_messages():
|
async def get_messages():
|
||||||
# get list of SMS messages from the Microhard modem
|
# get list of SMS messages from the Microhard modem
|
||||||
# parse them into a list and return
|
# parse them into a list and return
|
||||||
|
@ -120,6 +153,37 @@ async def check_messages():
|
||||||
await delete_message(message['mid'])
|
await delete_message(message['mid'])
|
||||||
|
|
||||||
|
|
||||||
|
@bot.on(events.NewMessage)
|
||||||
|
async def new_message(event):
|
||||||
|
reply_id = event.message.reply_to_msg_id
|
||||||
|
|
||||||
|
logging.info('=> Telegram - id: {}, reply_id: {}, sender: {} ({}), text: {}'.format(
|
||||||
|
event.message.id, reply_id, event.sender.first_name, event.sender_id, event.raw_text
|
||||||
|
))
|
||||||
|
|
||||||
|
if not reply_id:
|
||||||
|
logging.info(' Not a reply, ignoring')
|
||||||
|
return
|
||||||
|
|
||||||
|
if not event.raw_text:
|
||||||
|
logging.info(' No text, aborting.')
|
||||||
|
await event.reply('Error: No text found. Media not supported yet.', silent=True)
|
||||||
|
return
|
||||||
|
|
||||||
|
original = await event.message.get_reply_message()
|
||||||
|
|
||||||
|
logging.info('Original message contents:\n%s', original.raw_text)
|
||||||
|
|
||||||
|
from_field = original.raw_text.split('From: ', 1)[1].split()[0]
|
||||||
|
|
||||||
|
logging.info('Original number found: %s', from_field)
|
||||||
|
|
||||||
|
res = await send_message(from_field, event.raw_text)
|
||||||
|
|
||||||
|
if not res:
|
||||||
|
await event.reply('Error sending SMS.')
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
@ -135,7 +199,6 @@ async def main():
|
||||||
logging.error('Problem checking messages: {} - {}'.format(e.__class__.__name__, str(e)))
|
logging.error('Problem checking messages: {} - {}'.format(e.__class__.__name__, str(e)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.create_task(main())
|
loop.create_task(main())
|
||||||
|
|
Loading…
Reference in New Issue
Block a user