Skip to content

Instantly share code, notes, and snippets.

@NovikovAlexander
Last active September 30, 2023 11:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save NovikovAlexander/73aaa0c7881d57f2017aac19527c80a8 to your computer and use it in GitHub Desktop.
Save NovikovAlexander/73aaa0c7881d57f2017aac19527c80a8 to your computer and use it in GitHub Desktop.
Telegram bot on webhook with django and pyTelegramBotAPI
# Setting up Webhook
# https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/setWebhook?url=https://mysite.com/cbbf15d8-0421-4512-84d9-5e5d977e3aef/
# urls.py
from bot.views import bot
urlpatterns = [
path('cbbf15d8-0421-4512-84d9-5e5d977e3aef/', bot, name="bot"),
]
# views.py
from django.http import HttpResponse
from django.core.exceptions import PermissionDenied
from django.views.decorators.csrf import csrf_exempt
import telebot
TOKEN = '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11'
tbot = telebot.TeleBot(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(content_types=["text"])
def get_okn(message):
tbot.send_message(message.chat.id, "Hello, bot!")
@santahate
Copy link

Hello!
Tried to do the same but have error:
AttributeError: 'TeleBot' object has no attribute 'message_handler'

same problem with telebot.types

Thank you!

@Umidrifkatov
Copy link

Tried to do the same but have error:
AttributeError: 'TeleBot' object has no attribute 'message_handler'

from telebot import types
or reinstall pytelegrambotapi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment