Compare commits
No commits in common. "fef9f0f1c217fe515c44768ff0936a5fe24aafbd" and "be038a90f5174dd15630f92cdcf6353a1a9a9df0" have entirely different histories.
fef9f0f1c2
...
be038a90f5
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -106,4 +106,3 @@ ENV/
|
||||||
db.sqlite3
|
db.sqlite3
|
||||||
|
|
||||||
data/*
|
data/*
|
||||||
settings.py
|
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
<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="g/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
|
|
||||||
========
|
|
||||||
|
|
84
main.py
84
main.py
|
@ -1,84 +0,0 @@
|
||||||
import settings
|
|
||||||
import asyncio
|
|
||||||
import json
|
|
||||||
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 = {}
|
|
||||||
|
|
||||||
@bot.on(events.NewMessage(incoming=True))
|
|
||||||
async def new_message(event):
|
|
||||||
text = event.raw_text
|
|
||||||
sender = event.sender_id
|
|
||||||
print(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:
|
|
||||||
if not website.startswith('http://') and not website.startswith('https://'):
|
|
||||||
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/guestbook.html', 'a') as f:
|
|
||||||
f.write(entry)
|
|
||||||
|
|
||||||
await event.reply('Entry added to t0.vc/g')
|
|
||||||
print('Added', data)
|
|
||||||
|
|
||||||
|
|
||||||
async def submit(request):
|
|
||||||
data = dict(await request.post())
|
|
||||||
data['date'] = str(datetime.today().date())
|
|
||||||
mid = str(uuid4()).split('-')[0]
|
|
||||||
print(mid, data)
|
|
||||||
|
|
||||||
try:
|
|
||||||
name = data['name']
|
|
||||||
website = data.get('website', '')
|
|
||||||
message = data['message']
|
|
||||||
except KeyError:
|
|
||||||
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.')
|
|
||||||
|
|
||||||
report = 'Name: {}\n\nWebsite: {}\n\nMessage: {}\n\n/allow_{}'
|
|
||||||
await bot.send_message(TANNER, message=report.format(name, website, message, 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)
|
|
Loading…
Reference in New Issue
Block a user