├── .gitignore ├── LICENSE.md ├── README.md ├── __init__.py ├── _config.yml ├── bot.py ├── client_secrets.json ├── config.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | client_secrets.json 2 | storage.json 3 | env/ -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Sumithran V Babu 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Telegram bot Google Drive API integration example 2 | 3 | [![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.png?v=103)](https://github.com/ellerbrock/open-source-badges/) 4 | [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.png?v=103)](https://opensource.org/licenses/mit-license.php) 5 | 6 | This is a Telegram bot that uses **Google Drive API V3** Python Client to upload files to Google Drive. 7 | 8 | 9 | 10 | ## Setup 11 | 12 | To begin, you'll need Telegram API Access Token, And you could get it from [@BotFather](https://t.me/botfather). 13 | 14 | You have to enable [Google Drive API V3](https://console.developers.google.com/apis/library/drive.googleapis.com) 15 | 16 | 17 | ## Installation 18 | 19 | Clone the repository: 20 | 21 | ``` 22 | git clone https://github.com/zume2020/Telegram-bot-Google-Drive.git 23 | cd Telegram-bot-Google-Drive 24 | ``` 25 | 26 | Install dependencies 27 | 28 | ``` 29 | pip install -r requirements.txt 30 | ``` 31 | Run 32 | ``` 33 | python bot.py 34 | ``` 35 | ## License 36 | 37 | The project is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). 38 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zume2020/Telegram-bot-Google-Drive/8a8d3f26b469ca7b2b37800280deb50c36e58d39/__init__.py -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-architect -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from __future__ import print_function 4 | import os 5 | import config 6 | import pickle 7 | import telegram 8 | import logging 9 | from telegram.ext import Updater 10 | from telegram.ext import Updater, CommandHandler, CallbackQueryHandler ,MessageHandler 11 | from telegram.ext import MessageHandler, Filters 12 | from googleapiclient.http import MediaFileUpload 13 | from googleapiclient.discovery import build 14 | from google_auth_oauthlib.flow import InstalledAppFlow 15 | from google.auth.transport.requests import Request 16 | 17 | logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',level=logging.INFO) 18 | logger = logging.getLogger(__name__) 19 | 20 | 21 | def getCreds(): 22 | # The file token.pickle stores the user's access and refresh tokens, and is 23 | # created automatically when the authorization flow completes for the first 24 | # time. 25 | creds = None 26 | SCOPES = 'https://www.googleapis.com/auth/drive' 27 | 28 | if os.path.exists('token.pickle'): 29 | with open('token.pickle', 'rb') as token: 30 | creds = pickle.load(token) 31 | # If there are no (valid) credentials available, let the user log in. 32 | if not creds or not creds.valid: 33 | if creds and creds.expired and creds.refresh_token: 34 | creds.refresh(Request()) 35 | else: 36 | flow = InstalledAppFlow.from_client_secrets_file( 37 | 'credentials.json', SCOPES) 38 | creds = flow.run_local_server(port=0) 39 | # Save the credentials for the next run 40 | with open('token.pickle', 'wb') as token: 41 | pickle.dump(creds, token) 42 | 43 | return creds 44 | 45 | def start(update, context): 46 | context.bot.send_message(chat_id=update.effective_chat.id, text="Upload files here.") 47 | 48 | 49 | def file_handler(update, context): 50 | """handles the uploaded files""" 51 | 52 | file = context.bot.getFile(update.message.document.file_id) 53 | file.download(update.message.document.file_name) 54 | 55 | doc = update.message.document 56 | 57 | service = build('drive', 'v3', credentials=getCreds(),cache_discovery=False) 58 | filename = doc.file_name 59 | 60 | metadata = {'name': filename} 61 | media = MediaFileUpload(filename, chunksize=1024 * 1024, mimetype=doc.mime_type, resumable=True) 62 | request = service.files().create(body=metadata, 63 | media_body=media) 64 | 65 | response = None 66 | while response is None: 67 | status, response = request.next_chunk() 68 | if status: 69 | print( "Uploaded %d%%." % int(status.progress() * 100)) 70 | 71 | context.bot.send_message(chat_id=update.effective_chat.id, text="✅ File uploaded!") 72 | 73 | 74 | def silentremove(filename): 75 | try: 76 | os.remove(filename) 77 | except OSError: 78 | pass 79 | 80 | def error(bot, update, error): 81 | logger.warning('Update "%s" caused error "%s"', update, error) 82 | 83 | def main(): 84 | updater = Updater(token=config.TOKEN,use_context=True) 85 | dispatcher = updater.dispatcher 86 | updater.dispatcher.add_handler(CommandHandler('start', start)) 87 | dispatcher.add_handler(MessageHandler(Filters.document,file_handler)) 88 | updater.start_polling() 89 | 90 | if __name__ == '__main__': 91 | main() -------------------------------------------------------------------------------- /client_secrets.json: -------------------------------------------------------------------------------- 1 | {"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret":"ckGVrYYQ_GE7O4rL80ozlEXR","token_uri":"https://accounts.google.com/o/oauth2/token","client_email":"","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"],"client_x509_cert_url":"","client_id":"665081966568.apps.googleusercontent.com","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"}} -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | TOKEN = "TOKEN FROM BOTFATHER" -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | asn1crypto==0.24.0 2 | backports-abc==0.5 3 | cachetools==3.1.1 4 | certifi==2022.12.7 5 | cffi==1.12.3 6 | cryptography==3.3.2 7 | enum34==1.1.6 8 | future==0.17.1 9 | futures==3.1.1 10 | google-api-python-client==1.7.11 11 | google-auth==1.6.3 12 | google-auth-httplib2==0.0.3 13 | httplib2==0.19.0 14 | ipaddress==1.0.22 15 | oauth2client==4.1.3 16 | pyasn1==0.4.6 17 | pyasn1-modules==0.2.6 18 | pycparser==2.19 19 | python-telegram-bot==12.0.0 20 | rsa==4.7 21 | singledispatch==3.4.0.3 22 | six==1.12.0 23 | tornado==5.1.1 24 | uritemplate==3.0.0 25 | --------------------------------------------------------------------------------