From 42955d18866a39236058f492b0b1c0cf52ad0bf5 Mon Sep 17 00:00:00 2001 From: abdullaev388 Date: Wed, 11 May 2022 10:50:33 +0500 Subject: [PATCH] added examples of multibot --- .../asynchronous_telebot/multibot/README.MD | 17 ++++++ .../asynchronous_telebot/multibot/config.py | 6 ++ .../asynchronous_telebot/multibot/handlers.py | 15 +++++ .../asynchronous_telebot/multibot/main.py | 56 +++++++++++++++++++ .../multibot/nginx_conf.conf | 8 +++ examples/multibot/README.MD | 17 ++++++ examples/multibot/config.py | 6 ++ examples/multibot/handlers.py | 14 +++++ examples/multibot/main.py | 48 ++++++++++++++++ examples/multibot/nginx_conf.conf | 8 +++ 10 files changed, 195 insertions(+) create mode 100644 examples/asynchronous_telebot/multibot/README.MD create mode 100644 examples/asynchronous_telebot/multibot/config.py create mode 100644 examples/asynchronous_telebot/multibot/handlers.py create mode 100644 examples/asynchronous_telebot/multibot/main.py create mode 100644 examples/asynchronous_telebot/multibot/nginx_conf.conf create mode 100644 examples/multibot/README.MD create mode 100644 examples/multibot/config.py create mode 100644 examples/multibot/handlers.py create mode 100644 examples/multibot/main.py create mode 100644 examples/multibot/nginx_conf.conf diff --git a/examples/asynchronous_telebot/multibot/README.MD b/examples/asynchronous_telebot/multibot/README.MD new file mode 100644 index 0000000..3693d36 --- /dev/null +++ b/examples/asynchronous_telebot/multibot/README.MD @@ -0,0 +1,17 @@ +You probably have seen bots which allow you to send them token of your bot and then handle updates providing some functionality for your bot. +
+This type of bots are called multibots. They are created using webhooks. +
+ +This is the example of simple multibot.
+In order to reproduce this example you need to have domain and ssl connection. +
+If you have, go to config.py and specify your data. +
+There is also file called nginx_conf.conf, we will use nginx as proxy-server and this file is example nginx.conf file. +
+Make sure that server_name and port are the same in both config and nginx_conf +
+(nginx_conf.conf IS NOT complete, you would probably use tools like certbot to add ssl connection to it) +
+Also, in this example I used dictionary as tokens storage, but in production you should use database so that you can re-set webhooks in case bot restarts. \ No newline at end of file diff --git a/examples/asynchronous_telebot/multibot/config.py b/examples/asynchronous_telebot/multibot/config.py new file mode 100644 index 0000000..17e7141 --- /dev/null +++ b/examples/asynchronous_telebot/multibot/config.py @@ -0,0 +1,6 @@ +MAIN_BOT_TOKEN = "your_main_bot_token" + +WEBHOOK_HOST = "your_domain.com" +WEBHOOK_PATH = "telegram_webhook" +WEBAPP_HOST = "0.0.0.0" +WEBAPP_PORT = 3500 diff --git a/examples/asynchronous_telebot/multibot/handlers.py b/examples/asynchronous_telebot/multibot/handlers.py new file mode 100644 index 0000000..c98a6de --- /dev/null +++ b/examples/asynchronous_telebot/multibot/handlers.py @@ -0,0 +1,15 @@ +from telebot.async_telebot import AsyncTeleBot +from telebot import types + + +async def hello_handler(message: types.Message, bot: AsyncTeleBot): + await bot.send_message(message.chat.id, "Hi :)") + + +async def echo_handler(message: types.Message, bot: AsyncTeleBot): + await bot.send_message(message.chat.id, message.text) + + +def register_handlers(bot: AsyncTeleBot): + bot.register_message_handler(hello_handler, func=lambda message: message.text == 'Hello', pass_bot=True) + bot.register_message_handler(echo_handler, pass_bot=True) diff --git a/examples/asynchronous_telebot/multibot/main.py b/examples/asynchronous_telebot/multibot/main.py new file mode 100644 index 0000000..19d6666 --- /dev/null +++ b/examples/asynchronous_telebot/multibot/main.py @@ -0,0 +1,56 @@ +import asyncio + +from aiohttp import web +from telebot import types, util +from telebot.async_telebot import AsyncTeleBot +from handlers import register_handlers + +import config + +main_bot = AsyncTeleBot(config.MAIN_BOT_TOKEN) +app = web.Application() +tokens = {config.MAIN_BOT_TOKEN: True} + + +async def webhook(request): + token = request.match_info.get('token') + if not tokens.get(token): + return web.Response(status=404) + + if request.headers.get('content-type') != 'application/json': + return web.Response(status=403) + + json_string = await request.json() + update = types.Update.de_json(json_string) + if token == main_bot.token: + await main_bot.process_new_updates([update]) + return web.Response() + + from_update_bot = AsyncTeleBot(token) + register_handlers(from_update_bot) + await from_update_bot.process_new_updates([update]) + return web.Response() + + +app.router.add_post("/" + config.WEBHOOK_PATH + "/{token}", webhook) + + +@main_bot.message_handler(commands=['add_bot']) +async def add_bot(message: types.Message): + token = util.extract_arguments(message.text) + tokens[token] = True + + new_bot = AsyncTeleBot(token) + await new_bot.delete_webhook() + await new_bot.set_webhook(f"{config.WEBHOOK_HOST}/{config.WEBHOOK_PATH}/{token}") + + await new_bot.send_message(message.chat.id, "Webhook was set.") + + +async def main(): + await main_bot.delete_webhook() + await main_bot.set_webhook(f"{config.WEBHOOK_HOST}/{config.WEBHOOK_PATH}/{config.MAIN_BOT_TOKEN}") + web.run_app(app, host=config.WEBAPP_HOST, port=config.WEBAPP_PORT) + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/examples/asynchronous_telebot/multibot/nginx_conf.conf b/examples/asynchronous_telebot/multibot/nginx_conf.conf new file mode 100644 index 0000000..776fa4c --- /dev/null +++ b/examples/asynchronous_telebot/multibot/nginx_conf.conf @@ -0,0 +1,8 @@ +server { + server_name your_domain.com; + + location /telegram_webhook/ { + proxy_pass http://localhost:3500; + } + +} \ No newline at end of file diff --git a/examples/multibot/README.MD b/examples/multibot/README.MD new file mode 100644 index 0000000..3693d36 --- /dev/null +++ b/examples/multibot/README.MD @@ -0,0 +1,17 @@ +You probably have seen bots which allow you to send them token of your bot and then handle updates providing some functionality for your bot. +
+This type of bots are called multibots. They are created using webhooks. +
+ +This is the example of simple multibot.
+In order to reproduce this example you need to have domain and ssl connection. +
+If you have, go to config.py and specify your data. +
+There is also file called nginx_conf.conf, we will use nginx as proxy-server and this file is example nginx.conf file. +
+Make sure that server_name and port are the same in both config and nginx_conf +
+(nginx_conf.conf IS NOT complete, you would probably use tools like certbot to add ssl connection to it) +
+Also, in this example I used dictionary as tokens storage, but in production you should use database so that you can re-set webhooks in case bot restarts. \ No newline at end of file diff --git a/examples/multibot/config.py b/examples/multibot/config.py new file mode 100644 index 0000000..17e7141 --- /dev/null +++ b/examples/multibot/config.py @@ -0,0 +1,6 @@ +MAIN_BOT_TOKEN = "your_main_bot_token" + +WEBHOOK_HOST = "your_domain.com" +WEBHOOK_PATH = "telegram_webhook" +WEBAPP_HOST = "0.0.0.0" +WEBAPP_PORT = 3500 diff --git a/examples/multibot/handlers.py b/examples/multibot/handlers.py new file mode 100644 index 0000000..8fa4182 --- /dev/null +++ b/examples/multibot/handlers.py @@ -0,0 +1,14 @@ +from telebot import types, TeleBot + + +def hello_handler(message: types.Message, bot: TeleBot): + bot.send_message(message.chat.id, "Hi :)") + + +def echo_handler(message: types.Message, bot: TeleBot): + bot.send_message(message.chat.id, message.text) + + +def register_handlers(bot: TeleBot): + bot.register_message_handler(hello_handler, func=lambda message: message.text == 'Hello', pass_bot=True) + bot.register_message_handler(echo_handler, pass_bot=True) diff --git a/examples/multibot/main.py b/examples/multibot/main.py new file mode 100644 index 0000000..91e4673 --- /dev/null +++ b/examples/multibot/main.py @@ -0,0 +1,48 @@ +from flask import Flask +from flask import request, abort +from telebot import TeleBot, types, util +from handlers import register_handlers + +import config + +main_bot = TeleBot(config.MAIN_BOT_TOKEN) +app = Flask(__name__) +tokens = {config.MAIN_BOT_TOKEN: True} + + +@app.route(f"/{config.WEBHOOK_PATH}/", methods=['POST']) +def webhook(token: str): + if not tokens.get(token): + return abort(404) + + if request.headers.get('content-type') != 'application/json': + return abort(403) + + json_string = request.get_data().decode('utf-8') + update = types.Update.de_json(json_string) + if token == main_bot.token: + main_bot.process_new_updates([update]) + return '' + + from_update_bot = TeleBot(token) + register_handlers(from_update_bot) + from_update_bot.process_new_updates([update]) + return '' + + +@main_bot.message_handler(commands=['add_bot']) +def add_bot(message: types.Message): + token = util.extract_arguments(message.text) + tokens[token] = True + + new_bot = TeleBot(token) + new_bot.delete_webhook() + new_bot.set_webhook(f"{config.WEBHOOK_HOST}/{config.WEBHOOK_PATH}/{token}") + + new_bot.send_message(message.chat.id, "Webhook was set.") + + +if __name__ == '__main__': + main_bot.delete_webhook() + main_bot.set_webhook(f"{config.WEBHOOK_HOST}/{config.WEBHOOK_PATH}/{config.MAIN_BOT_TOKEN}") + app.run(host=config.WEBAPP_HOST, port=config.WEBAPP_PORT) diff --git a/examples/multibot/nginx_conf.conf b/examples/multibot/nginx_conf.conf new file mode 100644 index 0000000..776fa4c --- /dev/null +++ b/examples/multibot/nginx_conf.conf @@ -0,0 +1,8 @@ +server { + server_name your_domain.com; + + location /telegram_webhook/ { + proxy_pass http://localhost:3500; + } + +} \ No newline at end of file