Last active
September 30, 2023 11:55
-
-
Save NovikovAlexander/73aaa0c7881d57f2017aac19527c80a8 to your computer and use it in GitHub Desktop.
Telegram bot on webhook with django and pyTelegramBotAPI
This file contains 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
# 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!") |
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
Hello!
Tried to do the same but have error:
AttributeError: 'TeleBot' object has no attribute 'message_handler'
same problem with telebot.types
Thank you!