Last active
July 17, 2024 13:14
-
-
Save UlugbekMuslitdinov/023482dcf264785109ea67c1577a8146 to your computer and use it in GitHub Desktop.
How to connect PyTelegramBotAPI with Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bot.views import bot | |
from django.urls import path | |
urlpatterns = [ | |
path('ANY-RANDOM-LINK/', bot, name="bot"), | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import telebot | |
from django.core.exceptions import PermissionDenied | |
# =========================================================================================> | |
TOKEN = 'YOUR-TELGRAM-BOT-TOKEN' | |
tbot = telebot.AsyncTeleBot(TOKEN) | |
# For free PythonAnywhere accounts | |
# tbot = telebot.TeleBot(TOKEN, threaded=False) | |
@csrf_exempt | |
def bot(request): | |
if request.META['CONTENT_TYPE'] == 'application/json': | |
json_data = request.body.decode('utf-8') | |
update = telebot.types.Update.de_json(json_data) | |
tbot.process_new_updates([update]) | |
return HttpResponse("") | |
else: | |
raise PermissionDenied | |
# =========================================================================================> | |
@tbot.message_handler(commands=['start']) | |
def greet(m): | |
tbot.send_message(m.chat.id, "Hello") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment