telebotter / django-telegrambot

Simple app for Telegram bot in Django
BSD 3-Clause "New" or "Revised" License
14 stars 7 forks source link

WEBHOOK doesn't work! #8

Open mbnoimi opened 4 years ago

mbnoimi commented 4 years ago

Hi,

My project works fine in case I set POLLING while is doesn't work at all with WEBHOOK. I suspect I missed smoothing in settings.py but I couldn't figure it out.

May I get some help please?

NOTES:

  1. I'm still newbie in both django and django-telegrambot so forgive me for the silly questions.
  2. My project root is https://mydomain.com/api/
  3. As you can see I use REST framework which works fine in https://mydomain.com/api/
  4. I couldn't know where can I catch the errors regarding WEBHOOK but Apache always identify error #6 as [wsgi:error]
  5. I successfully generated mydomain-crt.pem using win-acme for Let's Encrypt; It works fine for django in general but I'm not sure about it for django-telegrambot
  6. I'm able to access https://mydomain.com/api/admin/django-telegrambot/ and see: image

settings.py

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxx'
DEBUG = False # NOTE: Dev mode

ALLOWED_HOSTS = [
    '.mydomain.com',
    '.mydomain.com.',
    '.mydomain.com',
    '.mydomain.com.',
]

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'django_telegrambot',
    'api',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'somebot.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'somebot.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# TODO: Internationalization
LANGUAGE_CODE = 'en'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static")

DJANGO_TELEGRAMBOT = {
# FIXME: Bot dosn't response in Webhook mode 
    'MODE' : 'WEBHOOK', # (Optional [str]) 
    'WEBHOOK_SITE' : 'https://mydomain.com',
    'WEBHOOK_PREFIX' : '/api/',  
    'WEBHOOK_CERTIFICATE' : 'C:/apache-certs/mydomain-crt.pem', 

    'BOTS' : [
        {
           'TOKEN': '1234567890:xxxxxxxxxxxxxxxxx',
        },
    ],

}

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10,

    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ]
}

MEDIA_URL = '/uploads/'
MEDIA_ROOT = os.path.join(BASE_DIR, "uploads")
mbnoimi commented 4 years ago

My project structure is:

Project
├── api
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── models.py
│   ├── serializers.py
│   ├── telegrambot.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── django_telegrambot
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── management
│   │   ├── commands
│   │   │   ├── botpolling.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   ├── __init__.py
│   │   └── __pycache__
│   ├── models.py
│   ├── mqbot.py
│   ├── static
│   │   ├── css
│   │   │   └── django_telegrambot.css
│   │   ├── img
│   │   └── js
│   │       └── django_telegrambot.js
│   ├── templates
│   │   └── django_telegrambot
│   │       ├── base.html
│   │       └── index.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── somebot
│   ├── asgi.py
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
│   └── wsgi_windows.py
├── manage.py
└── requirements.txt
lukruh commented 4 years ago

0-2: that's fine

3: The details/traceback of the wsgi:error would be intresting here. You can set the apache log level to debug, to get some more information about what happend inside the webserver. But you should also setup logging for django, to log the errors, that are handled by django or your apps. You can read here about configuration of logging in django: https://docs.djangoproject.com/en/3.0/topics/logging/ feel free to ask again if that didnt help.

4: I've used Letsencrypt certificates as well, they may be some requirements, but they work in general 5: That looks alright.

Sorry for the late response was really busy the last weeks.