0

I want to get the user id to send the user an image. I've found a code that seems to do that.

user = Message.from_user
print('You talk with user {} and his user ID: {} '.format(user['username'], user['id']))

I just want to get user-id. my code is like below.

import logging
import telegram
from telegram import chat
from telegram import message



from telegram.utils.helpers import *

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import Bot, message, Chat
from telegram import message, Message
import time



# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)


# Define a few command handlers. These usually take the two arguments update and
# context. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
    """Send a message when the command /start is issued."""
    for i in range(10):
        update.message.reply_text('Hi!')
        time.sleep(0.5)
    
    
    
   # Bot.send_document(chat_id="1853314468:AAHWcod_1sETfH_VM_YQBT2gDa4gif-OoYA", document=open('tests/test.png', 'rb'))


def help(update, context):
    """Send a message when the command /help is issued."""
    update.message.reply_text('Help!')


def echo(update, context):
    """Echo the user message."""
    update.message.reply_text(update.message.text)


def error(update, context):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, context.error)



def image(update, context):
    """Send a message when the command /help is issued."""                                                        
    user = Message.from_user
    print('You talk with user {} and his user ID: {} '.format(user['username'], user['id']))
    
    


def main():
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    # Make sure to set use_context=True to use the new context based callbacks
    # Post version 12 this will no longer be necessary
    updater = Updater(token="1234567890:ABCDEFGHIJKLMNOPQRSTUVWXYZ", use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("help", help))
    dp.add_handler(CommandHandler("image", image))

    # on noncommand i.e message - echo the message on Telegram
    dp.add_handler(MessageHandler(Filters.text, echo))

    # log all errors
    dp.add_error_handler(error)

    # Start the Bot
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()


if __name__ == '__main__':
    main()

when i run this code i get: 2021-07-11 22:15:09,993 - main - WARNING - Update "{'update_id': 430649322, 'message': {'delete_chat_photo': False, 'text': '/image', 'entities': [{'type': 'bot_command', 'offset': 0, 'length': 6}], 'photo': [], 'message_id': 205, 'supergroup_chat_created': False, 'chat': {'username': 'MYUSR', 'id': MYID, 'first_name': 'MYFN', 'last_name': 'MYLN', 'type': 'private'}, 'new_chat_photo': [], 'date': 1626025509, 'channel_chat_created': False, 'caption_entities': [], 'group_chat_created': False, 'new_chat_members': [], 'from': {'is_bot': False, 'first_name': 'A.', 'username': 'MYUSR', 'last_name': 'MYLN', 'id': MYID, 'language_code': 'en'}}}" caused error "'member_descriptor' object is not subscriptable"

I don't know why I get this error. I'm writing my codes in VsCode and Python 3.9.1 64Bit. Thanks for helping.

Ali
  • 1
  • Revoke your bot token that you posted and please remove the line `dp.add_error_handler(error)` - the error handler you're using hides the full traceback (and has been removed from the PTB example for quite a while now for that reason) – CallMeStag Jul 11 '21 at 19:46
  • Does this answer your question? [How To Obtain Username, First Name Or Last Name Of A Telegram User With Python-Telegram-Bot?](https://stackoverflow.com/questions/40986737/how-to-obtain-username-first-name-or-last-name-of-a-telegram-user-with-python-t) While [this answer](https://stackoverflow.com/a/41054788/10606962) doesn't address your specific question 100%, the linked documentation contains all the info you need to solve your problem ;) – CallMeStag Jul 11 '21 at 19:47

0 Answers0