From 350bf3b2ade8379a9d2d668d34f96ddf5a756e74 Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 15:23:18 +0530 Subject: [PATCH 01/15] Intital commit --- .gitignore | 4 ++++ Procfile | 1 + app.py | 39 +++++++++++++++++++++++++++++++++++++++ requirements.txt | 10 ++++++++++ 4 files changed, 54 insertions(+) create mode 100644 .gitignore create mode 100644 Procfile create mode 100644 app.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2364b63 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/Lib +/Scripts +/Include +pyvenv.cfg \ No newline at end of file diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..5282b7d --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web app.py \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..67766f0 --- /dev/null +++ b/app.py @@ -0,0 +1,39 @@ +from telegram.ext import Updater +from telegram.ext import CommandHandler, MessageHandler +import os +TOKEN = os.environ.get("TELEGRAM_ID") + +def start(update, context): + yourname = update.message.chat.first_name + + msg = "Hi"+yourname+"! Welcome to mimic bot." + context.bot.send_message(update.message.chat.id, msg) + +def mimic(update, context): + context.bot.send_message(update.message.chat.id, update.message.text) + +def details(update, context): + context.bot.send_message(update.message.chat.id, update.message) + +def error(update, context): + context.bot.send_message(update.message.chat.id, "OOps! Error encountered!") + +def main(): + updater = Updater(token=TOKEN) + + dp = updater.dispatcher + + dp.add_handler(CommandHandler("/start",start)) + dp.add_handler(MessageHandler(mimic)) + dp.add_handler(CommandHandler("/details",details)) + + dp.add_error_handler(error) + + updater.start_webhook(listen="0.0.0.0",port=os.environ.get("PORT",443), + url_path=TOKEN, + webhook_url="https://mimic-app@heroku.com/"+TOKEN) + updater.idle() + +if __name__ == '__main__': + main() + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1d7bf0a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,10 @@ +APScheduler==3.6.3 +cachetools==4.2.2 +certifi==2021.10.8 +python-telegram-bot==13.8.1 +pytz==2021.3 +pytz-deprecation-shim==0.1.0.post0 +six==1.16.0 +tornado==6.1 +tzdata==2021.5 +tzlocal==4.1 From 6e552246a2dd281a5d0d377663834f4c56bc525d Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 15:28:15 +0530 Subject: [PATCH 02/15] changes in procfile --- Procfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Procfile b/Procfile index 5282b7d..ab678e0 100644 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -web app.py \ No newline at end of file +web: python3 app.py \ No newline at end of file From 4293bb92218b5256939005cc59dca0f6289a2420 Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 15:30:02 +0530 Subject: [PATCH 03/15] changes in app.py --- app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 67766f0..bde1ed5 100644 --- a/app.py +++ b/app.py @@ -23,9 +23,9 @@ def main(): dp = updater.dispatcher - dp.add_handler(CommandHandler("/start",start)) + dp.add_handler(CommandHandler("start",start)) dp.add_handler(MessageHandler(mimic)) - dp.add_handler(CommandHandler("/details",details)) + dp.add_handler(CommandHandler("details",details)) dp.add_error_handler(error) From 670316c28fcb6f94133739b8dc76f2f1bfb19ded Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 15:34:05 +0530 Subject: [PATCH 04/15] changes in app.py --- app.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index bde1ed5..7496074 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,7 @@ from telegram.ext import Updater from telegram.ext import CommandHandler, MessageHandler import os + TOKEN = os.environ.get("TELEGRAM_ID") def start(update, context): @@ -19,7 +20,7 @@ def error(update, context): context.bot.send_message(update.message.chat.id, "OOps! Error encountered!") def main(): - updater = Updater(token=TOKEN) + updater = Updater(token=TOKEN, use_context=True) dp = updater.dispatcher From 8695f50bc73f309fc35336294339f1d7964cfe1d Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 15:38:03 +0530 Subject: [PATCH 05/15] changes in app.py --- app.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 7496074..41dd301 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,5 @@ from telegram.ext import Updater -from telegram.ext import CommandHandler, MessageHandler +from telegram.ext import CommandHandler, MessageHandler, Filters import os TOKEN = os.environ.get("TELEGRAM_ID") @@ -10,6 +10,7 @@ def start(update, context): msg = "Hi"+yourname+"! Welcome to mimic bot." context.bot.send_message(update.message.chat.id, msg) + def mimic(update, context): context.bot.send_message(update.message.chat.id, update.message.text) @@ -25,7 +26,7 @@ def main(): dp = updater.dispatcher dp.add_handler(CommandHandler("start",start)) - dp.add_handler(MessageHandler(mimic)) + dp.add_handler(MessageHandler(Filters.text, mimic)) dp.add_handler(CommandHandler("details",details)) dp.add_error_handler(error) From 56197669cdf7681d6337dbdeffae112b4a2f8022 Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 15:44:02 +0530 Subject: [PATCH 06/15] changes in app.py --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 41dd301..2960444 100644 --- a/app.py +++ b/app.py @@ -33,7 +33,7 @@ def main(): updater.start_webhook(listen="0.0.0.0",port=os.environ.get("PORT",443), url_path=TOKEN, - webhook_url="https://mimic-app@heroku.com/"+TOKEN) + webhook_url="https://mimic-app.herokuapp.com/"+TOKEN) updater.idle() if __name__ == '__main__': From 53d24b522e67db286b27ebe3fe1aa7c742dc140b Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 15:50:41 +0530 Subject: [PATCH 07/15] changes in app.py --- app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 2960444..13aa38c 100644 --- a/app.py +++ b/app.py @@ -7,7 +7,7 @@ def start(update, context): yourname = update.message.chat.first_name - msg = "Hi"+yourname+"! Welcome to mimic bot." + msg = "Hi "+yourname+"! Welcome to mimic bot." context.bot.send_message(update.message.chat.id, msg) @@ -15,7 +15,7 @@ def mimic(update, context): context.bot.send_message(update.message.chat.id, update.message.text) def details(update, context): - context.bot.send_message(update.message.chat.id, update.message) + context.bot.send_message(update.message.chat.id, update) def error(update, context): context.bot.send_message(update.message.chat.id, "OOps! Error encountered!") From ad76a115bd63d2e29cb26bb69f19a7ae724290c5 Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 15:57:06 +0530 Subject: [PATCH 08/15] changes in app.py --- app.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 13aa38c..a44ab57 100644 --- a/app.py +++ b/app.py @@ -15,7 +15,8 @@ def mimic(update, context): context.bot.send_message(update.message.chat.id, update.message.text) def details(update, context): - context.bot.send_message(update.message.chat.id, update) + print(update) + context.bot.send_message(update.message.chat.id, str(update)) def error(update, context): context.bot.send_message(update.message.chat.id, "OOps! Error encountered!") From 22ec3f81dd17b2da153fe06bab22887a2f44c389 Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 15:59:26 +0530 Subject: [PATCH 09/15] changes in app.py --- app.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index a44ab57..2ce30de 100644 --- a/app.py +++ b/app.py @@ -19,7 +19,7 @@ def details(update, context): context.bot.send_message(update.message.chat.id, str(update)) def error(update, context): - context.bot.send_message(update.message.chat.id, "OOps! Error encountered!") + context.bot.send_message(update.message.chat.id, "Oops! Error encountered!") def main(): updater = Updater(token=TOKEN, use_context=True) @@ -27,8 +27,9 @@ def main(): dp = updater.dispatcher dp.add_handler(CommandHandler("start",start)) + dp.add_handler(CommandHandler("details", details)) dp.add_handler(MessageHandler(Filters.text, mimic)) - dp.add_handler(CommandHandler("details",details)) + dp.add_error_handler(error) From 0402afc5fc6a90720b4f4a274186b998edc550ff Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 16:02:26 +0530 Subject: [PATCH 10/15] changes in app.py --- app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 2ce30de..c0a6ffe 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,7 @@ from telegram.ext import Updater from telegram.ext import CommandHandler, MessageHandler, Filters import os - +import json TOKEN = os.environ.get("TELEGRAM_ID") def start(update, context): @@ -16,7 +16,7 @@ def mimic(update, context): def details(update, context): print(update) - context.bot.send_message(update.message.chat.id, str(update)) + context.bot.send_message(update.message.chat.id, json.dumps(update,indent=3)) def error(update, context): context.bot.send_message(update.message.chat.id, "Oops! Error encountered!") From ad3aa36b5eee45c7662073c0d3f6559cc1914b73 Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 16:09:43 +0530 Subject: [PATCH 11/15] changes in app.py --- app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index c0a6ffe..307da49 100644 --- a/app.py +++ b/app.py @@ -15,8 +15,7 @@ def mimic(update, context): context.bot.send_message(update.message.chat.id, update.message.text) def details(update, context): - print(update) - context.bot.send_message(update.message.chat.id, json.dumps(update,indent=3)) + context.bot.send_message(update.message.chat.id, update) def error(update, context): context.bot.send_message(update.message.chat.id, "Oops! Error encountered!") @@ -28,6 +27,7 @@ def main(): dp.add_handler(CommandHandler("start",start)) dp.add_handler(CommandHandler("details", details)) + dp.add_handler(MessageHandler(Filters.text, mimic)) From 94da35100836fa116cdbd312df08983d751765b5 Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 16:13:45 +0530 Subject: [PATCH 12/15] changes in app.py --- app.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 307da49..f24c00e 100644 --- a/app.py +++ b/app.py @@ -15,7 +15,10 @@ def mimic(update, context): context.bot.send_message(update.message.chat.id, update.message.text) def details(update, context): - context.bot.send_message(update.message.chat.id, update) + print(type(update)) + obj = json.dumps(update, indent=3) + print(type(obj)) + context.bot.send_message(update.message.chat.id, obj) def error(update, context): context.bot.send_message(update.message.chat.id, "Oops! Error encountered!") From be9f9c5f2af308fabc064338b5cd87d312b2609b Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 16:16:09 +0530 Subject: [PATCH 13/15] changes in app.py --- app.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app.py b/app.py index f24c00e..adc88e9 100644 --- a/app.py +++ b/app.py @@ -15,10 +15,7 @@ def mimic(update, context): context.bot.send_message(update.message.chat.id, update.message.text) def details(update, context): - print(type(update)) - obj = json.dumps(update, indent=3) - print(type(obj)) - context.bot.send_message(update.message.chat.id, obj) + context.bot.send_message(update.message.chat.id, str(update)) def error(update, context): context.bot.send_message(update.message.chat.id, "Oops! Error encountered!") From d469425e96e47d167c6ef9c514d42be0244b06dd Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 21:49:19 +0530 Subject: [PATCH 14/15] Create README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..13879fe --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# Python-Heroku Tutorial + +This repository contains all the files required to deploy python application on Heroku. Heroku is a cloud plateform which enables the developers/user to deploy, scale, manage and montor +their application. + +In this project, a python telegram bot is deployed in Heroku. + +You can use the bot on telgram: @mimic_app_bot From e62553ff1c8a29c237238d343083aed69b3fb6ba Mon Sep 17 00:00:00 2001 From: Rohit joshi Date: Tue, 7 Dec 2021 22:12:48 +0530 Subject: [PATCH 15/15] Update app.py --- app.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index adc88e9..2a388ab 100644 --- a/app.py +++ b/app.py @@ -2,29 +2,40 @@ from telegram.ext import CommandHandler, MessageHandler, Filters import os import json + +#telegram token TOKEN = os.environ.get("TELEGRAM_ID") +#commandhandler for start command def start(update, context): yourname = update.message.chat.first_name msg = "Hi "+yourname+"! Welcome to mimic bot." context.bot.send_message(update.message.chat.id, msg) - +#Message handler for texts only def mimic(update, context): context.bot.send_message(update.message.chat.id, update.message.text) + +#commandhandler for details command def details(update, context): context.bot.send_message(update.message.chat.id, str(update)) +#Error handler def error(update, context): context.bot.send_message(update.message.chat.id, "Oops! Error encountered!") +#main logic def main(): + + #to get the updates from bot updater = Updater(token=TOKEN, use_context=True) - + + #to dispatch the updates to respective handlers dp = updater.dispatcher - + + #handlers dp.add_handler(CommandHandler("start",start)) dp.add_handler(CommandHandler("details", details)) @@ -32,12 +43,14 @@ def main(): dp.add_error_handler(error) - + + #to start webhook updater.start_webhook(listen="0.0.0.0",port=os.environ.get("PORT",443), url_path=TOKEN, webhook_url="https://mimic-app.herokuapp.com/"+TOKEN) updater.idle() +#start application with main function if __name__ == '__main__': main()