Add /send command for sending SMS

This commit is contained in:
Tanner Collin 2024-10-02 00:03:46 +00:00
parent dd6f476a3f
commit cc9fb225e1

35
main.py
View File

@ -9,6 +9,7 @@ import asyncio
from telnetlib import Telnet
import csv
import json
import re
import settings
@ -77,7 +78,7 @@ async def send_message(number, text):
logging.info('Successfully sent.')
return True
else:
logging.info('Problem sending: %s', res)
logging.info('Problem sending, response: %s,', res)
return False
@ -153,7 +154,37 @@ async def check_messages():
await delete_message(message['mid'])
@bot.on(events.NewMessage)
@bot.on(events.NewMessage(pattern='/send'))
async def send(event):
logging.info('Send command: %s', event.raw_text)
data = event.raw_text.split(' ', 1)
try:
_, number, message = event.raw_text.split(' ', 2)
except:
logging.info('Bad command args')
await event.reply('Bad command args, usage: /send [number] [message]')
return
if message[0].isdigit():
logging.info('Message starts with digit, possible spaces in number.')
await event.reply('Phone number may have a space, aborting.')
return
if not re.match(r'^\+?[1-9]\d{1,14}$', number):
logging.info('Bad number: %s', number)
await event.reply('Bad number character. Format: +1231231234 or 1231231234')
return
res = await send_message(number, message)
if res:
await event.reply('Ok.')
else:
await event.reply('Error sending SMS.')
@bot.on(events.NewMessage(pattern='^(?!\/).*$'))
async def new_message(event):
reply_id = event.message.reply_to_msg_id