100 lines
3.0 KiB
Python
100 lines
3.0 KiB
Python
|
|
from flask import Flask
|
|
from flask import request
|
|
from teleflask import Teleflask
|
|
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 pytgbot.api_types.sendable.reply_markup import InlineKeyboardButton,InlineKeyboardMarkup
|
|
|
|
app=Flask(__name__)
|
|
chat_id='108021014'
|
|
bot = Teleflask(API_KEY)
|
|
bot.init_app(app)
|
|
|
|
|
|
|
|
@app.route('/')
|
|
def home():
|
|
return "Home"
|
|
|
|
@app.route('/send/<text>')
|
|
def send(text=None):
|
|
|
|
bot.bot.send_message(chat_id, text)
|
|
return "Text: %s <br> %s" % (text,request.headers)
|
|
|
|
|
|
def download_file(url):
|
|
local_filename = url.split('/')[-1]
|
|
# NOTE the stream=True parameter below
|
|
with requests.get(url, stream=True) as r:
|
|
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):
|
|
|
|
from pytgbot.api_types.receivable.updates import 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))
|
|
|
|
|
|
app.run(host="0.0.0.0",port="5000",debug=True)
|