Skip to content

Commit 52193b6

Browse files
Added File Sharing Telegram Bot
1 parent 537132d commit 52193b6

File tree

6 files changed

+236
-0
lines changed

6 files changed

+236
-0
lines changed

File-Sharing-Bot/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Darshan Patel
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

File-Sharing-Bot/Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
worker: python bot.py

File-Sharing-Bot/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# File-Sharing-Bot
2+
File Sharing telegram Bot developed in Python
3+
It is like a centerlized file repository where authorized users can share files and files are available to all the users.
4+
Functionalities and commands can be seen using /help command.
5+
This bot can be directly hosted on Heroku.

File-Sharing-Bot/bot.py

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
2+
import logging
3+
import os
4+
import telegram
5+
import shutil
6+
# Enable logging
7+
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
8+
level=logging.INFO)
9+
10+
logger = logging.getLogger(__name__)
11+
12+
#list of authorized users
13+
#create a list of telegram usernames to authorise them, 0th username is admin.
14+
username_list = []
15+
# Define a few command handlers. These usually take the two arguments bot and
16+
# update. Error handlers also receive the raised TelegramError object in error.
17+
def start(bot, update):
18+
"""Send a message when the command /start is issued."""
19+
reply = "Welcome to World of Automation. \nI am a bot developed by a Lazy Programmer.\nSend /help command to see what i can do."
20+
update.message.reply_text(reply)
21+
22+
23+
def help(bot, update):
24+
"""Send a message when the command /help is issued."""
25+
admin = update.message.from_user.username
26+
if admin == username_list[0]:
27+
reply = '''Send /get folder_name/file_name.extension to receive a file.
28+
\nSend /ls folder_name to show list of files.
29+
\nSend /put folder_name/file_name.extension to upload last sent file.
30+
\nSend /mkdir folder_name to create a Folder.
31+
\nSend /remove folder_name/filename.extension to delete a file.
32+
\nSend /adduser username to give access.
33+
\nSend /removeuser username to revoke access.
34+
\nSend /showuser to show list of users
35+
'''
36+
else:
37+
reply = '''Send /get folder_name/file_name.extension to receive a file.
38+
\nSend /ls folder_name to show list of files.
39+
\nSend /put folder_name/file_name.extension to upload last sent file.
40+
\nSend /mkdir folder_name to create a Folder.
41+
'''
42+
update.message.reply_text(reply)
43+
44+
45+
def get(bot, update):
46+
"""Send requested file."""
47+
username = update.message.from_user.username
48+
if(username not in username_list):
49+
update.message.reply_text("You are not Authorized.")
50+
return
51+
file = update.message.text.split(" ")[-1]
52+
if(file == "/send"):
53+
update.message.reply_text("Invalid File name.")
54+
else:
55+
reply = "Findind and Sending a requested file to you. Hold on..."
56+
update.message.reply_text(reply)
57+
path = os.getcwd()+'/'+file
58+
if (os.path.exists(path)):
59+
bot.send_document(chat_id=update.message.chat_id,document=open(path, 'rb'), timeout = 100)
60+
else:
61+
update.message.reply_text("File not Found.")
62+
63+
def ls(bot, update):
64+
"""Show files in requested directory."""
65+
username = update.message.from_user.username
66+
if(username not in username_list):
67+
update.message.reply_text("You are not Authorized.")
68+
return
69+
file = update.message.text.split(" ")[-1]
70+
if(file == "/show"):
71+
update.message.reply_text("Invalid Directory name.")
72+
else:
73+
reply = "Findind and Sending a list of files to you. Hold on..."
74+
update.message.reply_text(reply)
75+
path = os.getcwd()+'/'+file
76+
if (os.path.exists(path)):
77+
update.message.reply_text(os.listdir(path))
78+
else:
79+
update.message.reply_text("Directory not Found.")
80+
81+
def put(bot, update):
82+
f = open(str(os.getcwd())+"/file", "r")
83+
file_id = f.read()
84+
f.close
85+
if file_id == "":
86+
update.message.reply_text("You didn't upload file.")
87+
else:
88+
new_file = bot.get_file(file_id)
89+
message = update.message.text.split(" ")
90+
path = message[-1]
91+
if len(path) < 1:
92+
update.message.reply_text("Enter Path correctly.")
93+
else:
94+
new_file.download(os.getcwd()+'/'+path)
95+
update.message.reply_text("File Stored.")
96+
97+
98+
def mkdir(bot, update):
99+
message = update.message.text.split(" ")
100+
if len(message) < 1 or message[-1] == "/mkdir":
101+
update.message.reply_text("Invalid Syntax. Refer syntax in help section.")
102+
return
103+
path = os.getcwd() + "/" + message[-1]
104+
os.mkdir(path)
105+
update.message.reply_text("Folder Created.")
106+
107+
108+
def echo(bot, update):
109+
"""Echo the user message."""
110+
if update.message.document:
111+
file_id = update.message.document.file_id
112+
f = open(str(os.getcwd())+"/file", "w")
113+
f.write(file_id)
114+
f.close
115+
update.message.reply_text("Received.Now send file name and location to store. using /put command")
116+
else:
117+
reply = "Invalid Input."
118+
update.message.reply_text(reply)
119+
120+
def error(bot, update, error):
121+
"""Log Errors caused by Updates."""
122+
logger.warning('Update "%s" caused error "%s"', update, error)
123+
124+
def add_user(bot, update):
125+
admin = update.message.from_user.username
126+
if admin == username_list[0]:
127+
username = update.message.text.split(" ")[-1]
128+
username_list.append(username)
129+
update.message.reply_text("User added.")
130+
else:
131+
update.message.reply_text("You are not Authorized.")
132+
133+
def show_user(bot, update):
134+
admin = update.message.from_user.username
135+
if admin == username_list[0]:
136+
update.message.reply_text(username_list)
137+
else:
138+
update.message.reply_text("You are not Authorized.")
139+
140+
def remove_user(bot, update):
141+
admin = update.message.from_user.username
142+
if admin == username_list[0]:
143+
username = update.message.text.split(" ")[-1]
144+
username_list.remove(username)
145+
update.message.reply_text("User Removed.")
146+
else:
147+
update.message.reply_text("You are not Authorized.")
148+
149+
def remove(bot, update):
150+
admin = update.message.from_user.username
151+
if admin == username_list[0]:
152+
filename = update.message.text.split(" ")[-1]
153+
os.remove(os.getcwd()+ "/" + filename)
154+
update.message.reply_text("File Removed.")
155+
else:
156+
update.message.reply_text("You are not Authorized.")
157+
158+
def rmdir(bot, update):
159+
admin = update.message.from_user.username
160+
if admin == username_list[0]:
161+
filename = update.message.text.split(" ")[-1]
162+
shutil.rmtree(os.getcwd()+ "/" + filename)
163+
update.message.reply_text("Folder Removed.")
164+
else:
165+
update.message.reply_text("You are not Authorized.")
166+
167+
def main():
168+
"""Start the bot."""
169+
# Create the EventHandler and pass it your bot's token.
170+
TOKEN = os.environ['TOKEN']
171+
updater = Updater(TOKEN)
172+
173+
# Get the dispatcher to register handlers
174+
dp = updater.dispatcher
175+
176+
# on different commands - answer in Telegram
177+
dp.add_handler(CommandHandler("start", start))
178+
dp.add_handler(CommandHandler("help", help))
179+
dp.add_handler(CommandHandler("get", get))
180+
dp.add_handler(CommandHandler("ls", ls))
181+
dp.add_handler(CommandHandler("put", put))
182+
dp.add_handler(CommandHandler("mkdir", mkdir))
183+
184+
#admin functionalities
185+
dp.add_handler(CommandHandler("adduser", add_user))
186+
dp.add_handler(CommandHandler("showuser", show_user))
187+
dp.add_handler(CommandHandler("removeUser", remove_user))
188+
dp.add_handler(CommandHandler("remove", remove))
189+
dp.add_handler(CommandHandler("rmdir", rmdir))
190+
191+
# on noncommand i.e message - echo the message on Telegram
192+
dp.add_handler(MessageHandler(Filters.document, echo))
193+
194+
# log all errors
195+
dp.add_error_handler(error)
196+
197+
# Start the Bot
198+
updater.start_polling()
199+
200+
# Run the bot until you press Ctrl-C or the process receives SIGINT,
201+
# SIGTERM or SIGABRT. This should be used most of the time, since
202+
# start_polling() is non-blocking and will stop the bot gracefully.
203+
updater.idle()
204+
205+
206+
if __name__ == '__main__':
207+
main()

File-Sharing-Bot/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-telegram-bot==10.1.0

File-Sharing-Bot/runtime.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-3.6.0

0 commit comments

Comments
 (0)