def handle_list_images(update): m = re.search('^\/mm_listimages (.*?)$', update["message"]["text"]) if m: image_list_string = databasecon.get_all_images(update, m.group(1)) else: image_list_string = databasecon.get_all_images(update) writestring = "All Images in the Database: {0}".format(image_list_string) telegram.send_message( update["message"]["chat"]["id"], writestring )
def handle_get_imageinfo(update): imagename = None m = re.search('^\/mm_getimageinfo (.*?)$', update["message"]["text"]) if m: imagename = m.group(1) if imagename: databasecon.get_image_info(imagename, update) else: telegram.send_message( update["message"]["chat"]["id"], "Can't find imagename. Be sure your message looks like this: \"/mm_getimageinfo <imagename>\"" )
def handle_send(update): image_name = str(update["message"]["text"]) link = databasecon.search_image(image_name, update) if link: telegram.send_photo( update["message"]["chat"]["id"], link, image_name ) else: telegram.send_message( update["message"]["chat"]["id"], "Image not found" )
def handle_search_image(update): searchname = None m = re.search('^\/mm_searchimage [A-Za-z0-9_]*', update["message"]["text"]) if m: searchname = m.group(1) telegram.send_message( update["message"]["chat"]["id"], databasecon.search_image(searchname, update) ) else: telegram.send_message( update["message"]["chat"]["id"], "Can't find searchname. Be sure your message looks like this: \"/mm_searchimage <searchname>\"" )
def handle_add_user(update): username = None name = None m = re.search('^\/mm_adduser (.*?) [A-Za-z0-9_]*', update["message"]["text"]) #Matching the username if m: username = m.group(1) m = re.search('^\/mm_adduser [A-Za-z0-9_]* (.*?)$', update["message"]["text"]) #Matching the user's name if m: name = m.group(1) if username is not None and name is not None: databasecon.add_user(username, name, update) else: telegram.send_message( update["message"]["chat"]["id"], "Can't find username or user's name. Be sure your message looks like this: \"/mm_adduser <username> <name>\" The second name should be the user's firstname" )
def handle_add_image(update): name = None link = None m = re.search('^\/mm_addimage (.+?) [A-Za-z0-9]*', update["message"]["text"]) #Matching the name if m: name = m.group(1) m = re.search('^\/mm_addimage [A-Za-z0-9]*\.[A-Za-z0-9]* (.*?)$', update["message"]["text"]) #Matching the link if m: link = m.group(1) if link is not None and name is not None: databasecon.add_image(link, name, update["message"]["from"]["username"], update) else: telegram.send_message( update["message"]["chat"]["id"], "Can't find imagename or link. Be sure your message looks like this: \"/mm_addimage <imagename> <imagelink>\" and don't forget the file extension for the imagename" )
def delete_image(imagename, update): if str(update["message"]["from"]["id"]) == str(os.environ["ADMIN_ID"]): db_con = connect_to_db() cur = db_con.cursor() cur.execute("delete from mm_image where lower(id)=lower(%s);", [imagename]) db_con.commit() disconnect_from_db(db_con, update) telegram.send_message( update["message"]["chat"]["id"], "Removed image {0}".format(imagename) ) else: telegram.send_message( update["message"]["chat"]["id"], "Only the admin is allowed to remove images!" )
def handle_update_imagelink(update): imagename = None newlink = None m = re.search('^\/mm_updateimagelink (.*?) [A-Za-z0-9_\-\.]*', update["message"]["text"]) if m: imagename = m.group(1) m = re.search('^\/mm_updateimagelink [A-Za-z0-9_\-\.]* (.*?)$', update["message"]["text"]) if m: newlink = m.group(1) if newlink and imagename: databasecon.update_imagelink(imagename, newlink, update) else: telegram.send_message( update["message"]["chat"]["id"], "Can't find imagename or new link. Be sure your message looks like this: \"/mm_updateimagelink <imagename> <new link>\"" )
def handle_update_imagename(update): oldname = None newname = None m = re.search('^\/mm_updateimagename (.*?) [A-Za-z0-9_]*', update["message"]["text"]) if m: oldname = m.group(1) m = re.search('^\/mm_updateimagename [A-Za-z0-9_\.]* (.*?)$', update["message"]["text"]) if m: newname = m.group(1) if oldname and newname: databasecon.update_imagename(oldname, newname, update) else: telegram.send_message( update["message"]["chat"]["id"], "Can't find old- or new name. Be sure your message looks like this: \"/mm_updateimagename <oldname> <newname>\"" )
def add_user(user_username, user_name, update): db_con = connect_to_db() cur = db_con.cursor() query = """select * from mm_user where username=%s""" cur.execute(query, [str(update["message"]["from"]["id"])]) result = cur.fetchone() disconnect_from_db(db_con, update) if str(update["message"]["from"]["id"]) == str(os.environ["ADMIN_ID"]) or result is not None: db_con = connect_to_db() cur = db_con.cursor() query = """select * from mm_user where username=%s""" cur.execute(query, [user_username]) result = cur.fetchone() if result == None: query = """insert into mm_user values(%s, %s)""" cur.execute(query, (user_username, user_name)) telegram.send_message( update["message"]["chat"]["id"], "User created!" ) db_con.commit() disconnect_from_db(db_con, update) else: telegram.send_message( update["message"]["chat"]["id"], "There's already a user with this id in the database" ) disconnect_from_db(db_con, update) else: telegram.send_message( update["message"]["chat"]["id"], "You are not allowed to add users!" )
def get_image_info(imagename, update): db_con = connect_to_db() cur = db_con.cursor() query = """select * from mm_user where username=%s""" cur.execute(query, [str(update["message"]["from"]["username"])]) result = cur.fetchone() disconnect_from_db(db_con, update) if str(update["message"]["from"]["id"]) == str(os.environ["ADMIN_ID"]) or result is not None: db_con = connect_to_db() cur = db_con.cursor() cur.execute("select * from mm_image where lower(id)=lower(%s)", [imagename]) result = cur.fetchone() disconnect_from_db(db_con, update) if result is not None: img_id = str(result[0]) img_link = str(result[1]) img_uploaded_by = str(result[2]) telegram.send_message( update["message"]["chat"]["id"], "The image {0} was:\nUploaded by: {1}\nAnd it points to: {2}".format(img_id, img_uploaded_by, img_link) ) else: telegram.send_message( update["message"]["chat"]["id"], "Couldn't find this image in the Databse" ) else: telegram.send_message( update["message"]["chat"]["id"], "You are not allowed to do that!" )
def update_imagename(oldname, newname, update): db_con = connect_to_db() cur = db_con.cursor() query = """select * from mm_user where username=%s""" cur.execute(query, [str(update["message"]["from"]["id"])]) result = cur.fetchone() disconnect_from_db(db_con, update) if str(update["message"]["from"]["id"]) == str(os.environ["ADMIN_ID"]) or result is not None: db_con = connect_to_db() cur = db_con.cursor() cur.execute("update mm_image set id=lower(%s) where lower(id)=lower(%s);", (newname, oldname)) db_con.commit() disconnect_from_db(db_con, update) telegram.send_message( update["message"]["chat"]["id"], "Updated image name {0} to {1}".format(oldname, newname) ) else: telegram.send_message( update["message"]["chat"]["id"], "You are not allowed to do that!" )
def update_imagelink(imagename, newlink, update): db_con = connect_to_db() cur = db_con.cursor() query = """select * from mm_image where uploaded_by=%s and id=%s""" cur.execute(query, (str(update["message"]["from"]["username"]), imagename)) result = cur.fetchone() disconnect_from_db(db_con, update) print(result) if str(update["message"]["from"]["id"]) == str(os.environ["ADMIN_ID"]) or result is not None: db_con = connect_to_db() cur = db_con.cursor() cur.execute("update mm_image set link=%s where lower(id)=lower(%s);", (newlink, imagename)) db_con.commit() disconnect_from_db(db_con, update) telegram.send_message( update["message"]["chat"]["id"], "Link updated!" ) else: telegram.send_message( update["message"]["chat"]["id"], "You didn't upload that image so you can't change it!" )
def add_image(image_link, image_name, user_id, update): db_con = connect_to_db() cur = db_con.cursor() query = """select * from mm_user where username=%s""" cur.execute(query, [str(update["message"]["from"]["username"])]) result = cur.fetchone() disconnect_from_db(db_con, update) if str(update["message"]["from"]["id"]) == str(os.environ["ADMIN_ID"]) or result is not None: if str(update["message"]["from"]["id"]) == str(os.environ["ADMIN_ID"]): if "ADMIN_USERNAME" in os.environ: user_id = str(os.environ["ADMIN_USERNAME"]) else: print("Missing ADMIN_USERNAME Environment-variable") db_con = connect_to_db() cur = db_con.cursor() query = """select * from mm_image where lower(id)=lower(%s)""" cur.execute(query, (image_name,)) result = cur.fetchone() if result == None: query = """insert into mm_image values(%s, %s, %s)""" cur.execute(query, (image_name, image_link, user_id)) db_con.commit() disconnect_from_db(db_con, update) telegram.send_message( update["message"]["chat"]["id"], "Image {0} successfully added".format(image_name) ) else: telegram.send_message( update["message"]["chat"]["id"], "There's already an image with this name in the database" ) disconnect_from_db(db_con, update) else: telegram.send_message( update["message"]["chat"]["id"], "You are not allowed to add images!" )
def handle_fuckyou(update): telegram.send_message( update["message"]["chat"]["id"], "F**k you too!" )
def handle_about(update): telegram.send_message( update["message"]["chat"]["id"], filehandler.get_doc(filehandler.Document.about) )
def handle_help(update): telegram.send_message( update["message"]["chat"]["id"], "Halp iz hia" )