0

I am working with python-telegram-bot building a menu system.

I created a Django project, as shown below, using Webhook to connect to Telegram.

When the first message is sent, I ask the contact for their phone number and keep this number so I don't need to ask for it in the next messages in the conversation, but it’s not working. I tried to store it in request.session, but apparently every new message is a new session and so I lose the number.

How can I solve this? Every help is welcome.

view.py

import json
from django.http.response import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from core.message import proccess


@csrf_exempt
def event(request):
    json_telegram = json.loads(request.body)
    proccess(request, json_telegram)
    return HttpResponse()

messages.py

import telegram
from bot_webhook.settings import TOKEN
from telegram import InlineKeyboardMarkup, InlineKeyboardButton

bot = telegram.Bot(token=TOKEN)


def proccess(request, json_telegram):
    if 'login' in request.session:
        msg_options(json_telegram)

    else:
        try:
            request.session['login'] = json_telegram['message']['contact']['phone_number']
            msg_options(json_telegram)

        except BaseException:
            msg_login(json_telegram)


def msg_login(json_telegram):
    chat_id = json_telegram['message']['from']['id']
    reply_markup = telegram.ReplyKeyboardMarkup(
        [[telegram.KeyboardButton('Click to Login', request_contact=True)]],
        resize_keyboard=True,
        one_time_keyboard=True
    )
    bot.sendMessage(chat_id, 'I need to authorize your access.', reply_markup=reply_markup)


def msg_options(json_telegram):
    chat_id = json_telegram['message']['from']['id']
    first_name = json_telegram['message']['from']['first_name']
    last_name = json_telegram['message']['from']['last_name']
    button_list = []
    button_list.append(InlineKeyboardButton('Button One', callback_data='query_one'))
    button_list.append(InlineKeyboardButton('Button two', callback_data='query_two'))
    reply_markup = InlineKeyboardMarkup(build_menu(button_list, n_cols=2))
    bot.send_message(text='Hello {0} {1}!\nI have this options:'.format(first_name, last_name),
                     chat_id=chat_id,
                     reply_markup=reply_markup)


def build_menu(buttons,
               n_cols,
               header_buttons=None,
               footer_buttons=None):
    menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
    if header_buttons:
        menu.insert(0, [header_buttons])
    if footer_buttons:
        menu.append([footer_buttons])
    return menu
Antonio José
  • 473
  • 2
  • 5
  • 14

1 Answers1

0

Once I haven't found a solution in Api, I worked with this solution:

When there is an interaction, I check if the contact already exists in my database, if positive I continue the conversation, if negative I ask for identification by sharing the phone. So when I get the information, I record it in the database, so that in a new interaction I can identify it automatically.

Record it in my database is it a option, another option is as text archive.

If you have a better option, please show it for us.

Antonio José
  • 473
  • 2
  • 5
  • 14