Compare commits

...

14 Commits

Author SHA1 Message Date
tanner 2f2bc581d4 Add captcha 2024-01-04 06:15:12 +00:00
tanner 61225ebd20 Fix bug from capitals in 'https://' 2023-04-29 19:38:05 +00:00
tanner 7943e291fb Add entries to top of guestbook instead 2023-04-19 04:29:41 +00:00
tanner 9220022d3e Add filters 2022-08-11 20:40:15 +00:00
tanner 5509264136 Improve filters 2022-07-27 05:09:40 +00:00
tanner da6efccf86 Freeze requirements 2022-06-18 04:17:37 +00:00
tanner 7eba92016e Send controller message on error, change hr line 2022-06-17 22:32:56 +00:00
tanner b420720a2c Improve logging, url prefixes 2022-01-07 00:19:01 +00:00
tanner 08d9de8784 Rename html/ and add error message 2021-08-20 22:14:57 +00:00
tanner 4a0d12971b Rename main.py to t0sig.py 2021-08-20 21:52:08 +00:00
tanner 95720ce0db Rename guestbook.html to index.html 2021-08-20 21:46:30 +00:00
tanner 9b50602873 Keep data/html 2021-08-20 21:45:01 +00:00
tanner fef9f0f1c2 Complete guest book 2021-08-20 21:41:04 +00:00
tanner 8d71f71a1d Start basic telethon bot + aiohttp server 2021-08-20 20:54:26 +00:00
7 changed files with 4437 additions and 0 deletions
+1
View File
@@ -106,3 +106,4 @@ ENV/
db.sqlite3
data/*
settings.py
View File
+23
View File
@@ -0,0 +1,23 @@
<meta charset=UTF-8><link rel=icon href=data:,><pre>
Tanner Collin
Guest Book
==========
If you visited my website, please sign my guestbook!
<form action="submit" method="POST" accept-charset="UTF-8">
Your name:
<input name="name">
Your website (optional):
<input name="website">
Your message:
<textarea name="message" cols="60" rows="8"></textarea>
<button type="submit">Submit</button>
</form>
Messages
========
+2143
View File
File diff suppressed because it is too large Load Diff
+17
View File
@@ -0,0 +1,17 @@
aiohttp==3.6.3
async-timeout==3.0.1
attrs==21.2.0
certifi==2021.10.8
chardet==3.0.4
idna==2.10
idna-ssl==1.1.0
multidict==4.7.6
pkg-resources==0.0.0
pyaes==1.6.1
pyasn1==0.4.8
requests==2.25.1
rsa==4.7.2
Telethon==1.23.0
typing-extensions==3.10.0.0
urllib3==1.26.8
yarl==1.5.1
+2120
View File
File diff suppressed because it is too large Load Diff
+133
View File
@@ -0,0 +1,133 @@
import os
import logging
logging.basicConfig(
format='[%(asctime)s] %(levelname)s %(module)s/%(funcName)s - %(message)s',
level=logging.DEBUG if os.environ.get('DEBUG') else logging.INFO)
import settings
import asyncio
import json
import requests
from datetime import datetime
from uuid import uuid4
from telethon import TelegramClient, events
from aiohttp import web
bot = TelegramClient('data/bot', settings.API_ID, settings.API_HASH).start(bot_token=settings.API_TOKEN)
TANNER = 79316791
messages = {}
def controller_message(message):
payload = dict(misc=message)
r = requests.post('https://tbot.tannercollin.com/message', data=payload, timeout=10)
if r.status_code == 200:
return True
else:
logging.exception('Unable to communicate with controller! Message: ' + message)
return False
@bot.on(events.NewMessage(incoming=True))
async def new_message(event):
text = event.raw_text
sender = event.sender_id
logging.info('{} {}'.format(sender, text))
if sender != TANNER:
return
if not text.startswith('/allow_'):
return
mid = text.replace('/allow_', '')
try:
data = messages[mid]
except KeyError:
await event.reply('Message ID not found. Did the bot restart?')
return
entry = '\n\n{} - {}'.format(data['date'], data['name'])
website = data['website']
if website:
prefixes = ['http://', 'https://', 'gemini://', 'gopher://']
for prefix in prefixes:
if website.lower().startswith(prefix):
break
else: # for loop
website = 'http://' + website
entry += ' (<a href="{0}" target="_blank" rel="noreferrer noopener">{0}</a>)'.format(website)
entry += '\n\n{}\n\n------------------------------------------------------------'.format(data['message'])
with open('data/g/index.html', 'r') as f:
page = f.read()
with open('data/g/index.html', 'w') as f:
header, entries = page.split('===========', maxsplit=1)
f.write(header)
f.write('===========')
f.write(entry)
f.write(entries)
await event.reply('Entry added to t0.vc/g')
logging.info('Added: {}'.format(data))
async def message_tanner(name, website, message, captcha, mid):
if 'tanner' not in captcha.lower():
return
if name.replace(' ', '') in ['website', 'webpage', 'homepage']:
return
report = 'Name: {}\n\nWebsite: {}\n\nMessage: {}\n\n/allow_{}'
try:
await bot.send_message(TANNER, message=report.format(name, website, message, mid))
except:
logging.error('Problem sending bot message.')
controller_message('t0sig: problem sending bot message!')
exit()
async def submit(request):
data = dict(await request.post())
data['date'] = str(datetime.today().date())
mid = str(uuid4()).split('-')[0]
logging.info('{} {}'.format(mid, data))
try:
fake_username = data.get('fake_username', '') # not used yet
name = data['name']
website = data.get('website', '')
message = data['message']
captcha = data.get('captcha', '')
except KeyError:
raise web.HTTPBadRequest(reason='You are missing something.')
if not len(name) or not len(message):
raise web.HTTPBadRequest(reason='You are missing something.')
if len(name) > 50:
raise web.HTTPBadRequest(reason='Name is too long.')
if len(website) > 100:
raise web.HTTPBadRequest(reason='Website is too long.')
if len(message) > 1000:
raise web.HTTPBadRequest(reason='Message is too long.')
await message_tanner(name, website, message, captcha, mid)
messages[mid] = data
with open('data/messages.log', 'a') as f:
f.write(json.dumps(data)+'\n')
return web.Response(text='Thanks! Your message is pending approval.')
if __name__ == '__main__':
bot.start()
app = web.Application()
app.router.add_post('/', submit)
web.run_app(app, port=8123)