Skip to content

Instantly share code, notes, and snippets.

@UlugbekMuslitdinov
Last active January 22, 2024 03:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save UlugbekMuslitdinov/023482dcf264785109ea67c1577a8146 to your computer and use it in GitHub Desktop.
Save UlugbekMuslitdinov/023482dcf264785109ea67c1577a8146 to your computer and use it in GitHub Desktop.
How to connect PyTelegramBotAPI with Django
from bot.views import bot
from django.urls import path
urlpatterns = [
path('ANY-RANDOM-LINK/', bot, name="bot"),
]
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