M
M
Meksvinz2018-07-03 14:16:41
Django
Meksvinz, 2018-07-03 14:16:41

Trouble with telebot + Django. Is it possible to attach a telegram bot to Django?

Hey!
I'm trying to connect a telegram bot and a Django admin panel. Django models described. everything works, but the bot does not want to start! Can you tell me where I took a wrong turn?
My steps:
1) Set a webhook to a domain with a certificate ( https://mydomain.com/bot)
2) Django urls.py resulted in this:

from django.contrib import admin
from django.urls import path
from bot import views as bot

urlpatterns = [
    path('admin/', admin.site.urls),
    path('bot/', bot.stuff),
]

3) In views.py I wrote the following:
from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
import telebot

@csrf_exempt
def stuff(message):
    bot = telebot.TeleBot(МОЙ ТОКЕН)
    @bot.message_handler(content_types=['text'])
    def echo(message):
        bot.send_message(message.chat.id, message.text)

    return HttpResponse("I`m not working like that!")

At first, I did not write the @csrf_exempt decorator, but without it, getWebhookInfo returned a 403 error:
{"ok":true,<br>
"result":{"url":"https://mydomain.com/bot/",<br>
"has_custom_certificate":false,<br>
"pending_update_count":1,<br>
"last_error_date":1530615883,<br>
"last_error_message":"Wrong response from the webhook: 403 Forbidden",<br>
"max_connections":40}}

But after adding @csrf_exempt, the situation is not at all rosy:
{"ok":true,"
result":{"url":" https://mydomain.com/bot/ ",
"has_custom_certificate":false,
"pending_update_count" :0,
"last_error_date":1530616116,
"last_error_message":"Connection refused",
"max_connections":40}}
Why does Connection refused pop up?
Please tell me what I did wrong, and how to get the simplest echo bot in general. I would be very grateful for clarifications and links to materials on the topic.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
novikov_alexandr, 2019-06-14
@novikov_alexandr

django-cors-headers are not needed
Here is a working version.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question