refactor major

This commit is contained in:
www
2020-12-02 00:04:39 +00:00
parent 58be5c779a
commit 894a99cdef
110 changed files with 20639 additions and 155 deletions

View File

@@ -6,29 +6,52 @@ from teleflask.messages import TextMessage
from key import API_KEY
import logging
import yaml
logging.basicConfig(level=logging.INFO)
URL_HOSTNAME='bot.2020.fet.at'
import requests
from bot1.states import CreatePostWorkflow
#from bot1.states import CreatePostWorkflow
from pytgbot.api_types.sendable.reply_markup import InlineKeyboardButton,InlineKeyboardMarkup
import os
from bot1.chats import ChatManager
from bot1 import users
#logging.basicConfig(level=logging.INFO)
URL_HOSTNAME='bot.2020.fet.at'
os.environ['URL_HOSTNAME']='bot.2020.fet.at'
app=Flask(__name__)
chat_id='108021014'
bot = Teleflask(API_KEY)
bot.init_app(app)
chats=ChatManager(bot.bot)
@app.route('/')
def home():
return "Home"
@app.route('/send/<text>')
def send(text=None):
def send(text=None):
bot.bot.send_message(chat_id, text)
return "Text: %s <br> %s" % (text,request.headers)
@app.route('/send/<chat_id>/<text>')
def send_to(chat_id,text=None):
print("sending:%s" %chat_id)
chats.send(chat_id,text)
return "Text: %s <br> %s" % (text,request.headers)
@app.route('/auth/<user_id>/<token>')
def auth_user(user_id, token):
if user_id in users.data:
print("known user")
u=users[user_id]
if token == u.token:
u.user=request.headers["Remote-User"]
u.fet_user=True
users.to_file()
print("authenticating")
return "development"
def download_file(url):
local_filename = url.split('/')[-1]
@@ -37,33 +60,10 @@ def download_file(url):
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
# If you have chunk encoded response uncomment if
# and set chunk_size parameter to None.
#if chunk:
f.write(chunk)
return local_filename
@bot.command("start")
def start(update, text):
m=update.message.to_array()
fn=m["from"]["first_name"]
return bot.bot.send_message(m["chat"]["id"],"<b>Hello</b> "+fn, parse_mode='html',reply_to_message_id=None)
def send_confirm( chat_id, text:str):
keyboard = [[InlineKeyboardButton("OK", callback_data='ok'),
InlineKeyboardButton("Repeat", callback_data='retry')],
[ InlineKeyboardButton("Cancel", callback_data='cancel')] ]
bot.bot.send_message(chat_id=chat_id,
text=text,
reply_markup=InlineKeyboardMarkup(keyboard))
@bot.command("confirm")
def confirm(update,text):
send_confirm(update.message.chat.id, "Please Confirm")
@bot.on_update
def bot_update(update):
@@ -71,29 +71,6 @@ def bot_update(update):
assert isinstance(update, Update)
print(yaml.dump(update.to_array()))
message=update.message
if message and message.entities and update.message.entities[0].type=="bot_command":
return bot.bot.send_msg(text="Hello World",chat_id=message.chat.id,reply_to_message_id=update.message.message_id)
if message and message.photo:
ff=max(update.message.photo, key=lambda x: x.file_size)
fff = bot.bot.get_file(ff.file_id)
download_file(fff.get_download_url(API_KEY))
return TextMessage("I download it!: %s" % str(fff.get_download_url(API_KEY)))
if update.callback_query:
bot.bot.edit_message_text(
chat_id=update.callback_query.message.chat.id,
message_id=update.callback_query.message.message_id,
text=update.callback_query.message.text+": "+update.callback_query.data)
if not update.message:
return
return TextMessage("Message: %s" % str(update.message))
chats.process_update(update)
app.run(host="0.0.0.0",port="5000",debug=True)