python-telegram-bot | We have made you a wrapper you can't refuse | Chat library

 by   python-telegram-bot Python Version: 20.2 License: GPL-3.0

kandi X-RAY | python-telegram-bot Summary

python-telegram-bot is a Python library typically used in Messaging, Chat applications. python-telegram-bot has no bugs, it has no vulnerabilities, it has build file available, it has a Strong Copyleft License and it has high support. You can install using 'pip install python-telegram-bot' or download it from GitHub, PyPI.
We have made you a wrapper you can't refuse
    Support
      Quality
        Security
          License
            Reuse
            Support
              Quality
                Security
                  License
                    Reuse

                      kandi-support Support

                        summary
                        python-telegram-bot has a highly active ecosystem.
                        summary
                        It has 21466 star(s) with 4685 fork(s). There are 596 watchers for this library.
                        summary
                        There were 5 major release(s) in the last 6 months.
                        summary
                        There are 16 open issues and 1847 have been closed. On average issues are closed in 4 days. There are 7 open pull requests and 0 closed requests.
                        summary
                        It has a negative sentiment in the developer community.
                        summary
                        The latest version of python-telegram-bot is 20.2
                        python-telegram-bot Support
                          Best in #Chat
                            Average in #Chat
                            python-telegram-bot Support
                              Best in #Chat
                                Average in #Chat

                                  kandi-Quality Quality

                                    summary
                                    python-telegram-bot has 0 bugs and 0 code smells.
                                    python-telegram-bot Quality
                                      Best in #Chat
                                        Average in #Chat
                                        python-telegram-bot Quality
                                          Best in #Chat
                                            Average in #Chat

                                              kandi-Security Security

                                                summary
                                                python-telegram-bot has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
                                                summary
                                                python-telegram-bot code analysis shows 0 unresolved vulnerabilities.
                                                summary
                                                There are 0 security hotspots that need review.
                                                python-telegram-bot Security
                                                  Best in #Chat
                                                    Average in #Chat
                                                    python-telegram-bot Security
                                                      Best in #Chat
                                                        Average in #Chat

                                                          kandi-License License

                                                            summary
                                                            python-telegram-bot is licensed under the GPL-3.0 License. This license is Strong Copyleft.
                                                            summary
                                                            Strong Copyleft licenses enforce sharing, and you can use them when creating open source projects.
                                                            python-telegram-bot License
                                                              Best in #Chat
                                                                Average in #Chat
                                                                python-telegram-bot License
                                                                  Best in #Chat
                                                                    Average in #Chat

                                                                      kandi-Reuse Reuse

                                                                        summary
                                                                        python-telegram-bot releases are available to install and integrate.
                                                                        summary
                                                                        Deployable package is available in PyPI.
                                                                        summary
                                                                        Build file is available. You can build the component from source.
                                                                        summary
                                                                        python-telegram-bot saves you 16930 person hours of effort in developing the same functionality from scratch.
                                                                        summary
                                                                        It has 50544 lines of code, 3701 functions and 312 files.
                                                                        summary
                                                                        It has medium code complexity. Code complexity directly impacts maintainability of the code.
                                                                        python-telegram-bot Reuse
                                                                          Best in #Chat
                                                                            Average in #Chat
                                                                            python-telegram-bot Reuse
                                                                              Best in #Chat
                                                                                Average in #Chat
                                                                                  Top functions reviewed by kandi - BETA
                                                                                  kandi has reviewed python-telegram-bot and discovered the below as its top functions. This is intended to give you an instant insight into python-telegram-bot implemented functionality, and help decide if they suit your requirements.
                                                                                  • Use this method to send an invoice
                                                                                    • Generic POST operation
                                                                                    • Do a POST request
                                                                                    • Sends a message to the endpoint
                                                                                  • Use this method to send a video
                                                                                    • Returns True if obj is a local file
                                                                                    • Parse a file input
                                                                                  • Use this method to send audio
                                                                                    • Parse file input
                                                                                  • Use this method to send a location
                                                                                  • Use this method to copy a message
                                                                                  • Add a sticker to a set
                                                                                  • Use this method to send a photo
                                                                                  • Use this method to send a contact message
                                                                                  • Answer an inline query
                                                                                  • Set webhook
                                                                                  • Edit a message live location
                                                                                  • Promote a chat member
                                                                                  • Use this method to send a video note
                                                                                  • Create a new sticker set
                                                                                  • Use this method to send a message
                                                                                  • Use this method to send a venue
                                                                                  • Use this method to send an animation
                                                                                  • Use this method to send a poll message
                                                                                  • Use this method to send a voice message
                                                                                  • Create an invoice link
                                                                                  Get all kandi verified functions for this library.
                                                                                  Get all kandi verified functions for this library.

                                                                                  python-telegram-bot Key Features

                                                                                  We have made you a wrapper you can't refuse

                                                                                  python-telegram-bot Examples and Code Snippets

                                                                                  python-telegram-bot: Pass arguments to the bot
                                                                                  Pythondot imgLines of Code : 24dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                  copy iconCopy
                                                                                  import os
                                                                                  import telegram
                                                                                  from telegram.ext import Updater, CommandHandler
                                                                                  
                                                                                  # --- init ---
                                                                                  
                                                                                  TOKEN = os.getenv('TELEGRAM_TOKEN')
                                                                                  
                                                                                  updater = Updater(token=TOKEN, use_context=True)
                                                                                  dispatcher = updater.dispatcher
                                                                                  
                                                                                  # --- commands ---
                                                                                  
                                                                                  def start(update, context):
                                                                                      print('text:', update.message.text)   # /start something
                                                                                      print('args:', context.args)          # ['something']
                                                                                  
                                                                                  dispatcher.add_handler(CommandHandler('start', start))
                                                                                  
                                                                                  # --- start ---
                                                                                      
                                                                                  updater.start_polling()
                                                                                  updater.idle()
                                                                                  
                                                                                  How I can do a send message task inside a dispatcher.run_async() using python-telegram-bot?
                                                                                  Pythondot imgLines of Code : 27dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                  copy iconCopy
                                                                                  dispatcher.run_async(myFunction, updater.bot)
                                                                                  
                                                                                  def myfunction(bot):
                                                                                      bot.send_message(text='Hello, World',chat_id=123456789)
                                                                                  
                                                                                  import telegram
                                                                                  from telegram.ext import Updater
                                                                                  
                                                                                  def myfunction(bot):
                                                                                      bot.send_message(text='Hello, World',chat_id=123456789)
                                                                                  
                                                                                  def main():
                                                                                      """Start the bot."""
                                                                                      # Create the Updater and pass it your bot's token.
                                                                                      updater = Updater("")
                                                                                  
                                                                                      # Get the dispatcher to register handlers
                                                                                      dispatcher = updater.dispatcher
                                                                                      dispatcher.run_async(myfunction, updater.bot)
                                                                                  
                                                                                      # Start the Bot
                                                                                      updater.start_polling()
                                                                                      updater.idle()
                                                                                  
                                                                                  if __name__ == '__main__':
                                                                                      main()
                                                                                  
                                                                                  Receive the typed value from the user after the command in python telegram bot
                                                                                  Pythondot imgLines of Code : 4dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                  copy iconCopy
                                                                                  import sys
                                                                                  print 'Number of arguments:', len(sys.argv), 'arguments.'
                                                                                  print 'Argument List:', str(sys.argv)
                                                                                  
                                                                                  Receive the typed value from the user after the command in python telegram bot
                                                                                  Pythondot imgLines of Code : 11dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                  copy iconCopy
                                                                                  def chart(update: Update, context: CallbackContext) -> None:
                                                                                      """Send a message with the arguments passed by the user, when the command /chart is issued."""
                                                                                      input_mex = update.message.text
                                                                                      input_args = input_mex.split('/chart ')[1]
                                                                                      update.message.reply_text(input_args)
                                                                                  
                                                                                  updater = Updater(token=TOKEN, use_context=True)
                                                                                  # Get the dispatcher to register handlers
                                                                                  dispatcher = updater.dispatcher
                                                                                  dispatcher.add_handler(CommandHandler('chart', chart))
                                                                                  
                                                                                  telegram bot python specific day automated messages
                                                                                  Pythondot imgLines of Code : 15dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                  copy iconCopy
                                                                                  from datetime import date
                                                                                  
                                                                                  messages = {
                                                                                      "03-20": "A message for March Equinox here",
                                                                                      "09-22": "A message for September Equinox here"
                                                                                  }
                                                                                  
                                                                                  
                                                                                  def get_message():
                                                                                      mm_dd = date.today().strftime("%m-%d")
                                                                                      return messages.get(mm_dd)
                                                                                  
                                                                                  
                                                                                  print(get_message())
                                                                                  
                                                                                  How to hide telegram.vendor.ptb error and warning messages?
                                                                                  Pythondot imgLines of Code : 40dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                  copy iconCopy
                                                                                  import logging
                                                                                  import (other modules)
                                                                                  
                                                                                  logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                                                                                                  level=logging.INFO)
                                                                                  
                                                                                  logging.getLogger("telegram.vendor.ptb_urllib3.urllib3").setLevel(logging.ERROR)
                                                                                  logger = logging.getLogger(__name__)
                                                                                  
                                                                                  def main():
                                                                                  
                                                                                   """Start the bot."""
                                                                                   
                                                                                   global token, allowedUsers
                                                                                  
                                                                                   # Create the Updater and pass it your bot's token.
                                                                                   updater = Updater(token)
                                                                                  
                                                                                   # Get the dispatcher to register handlers
                                                                                   dispatcher = updater.dispatcher
                                                                                  
                                                                                   # Registering handlers
                                                                                  
                                                                                   dispatcher.add_handler(CommandHandler("start", start))
                                                                                   dispatcher.add_handler(CommandHandler("help", help_info))
                                                                                   dispatcher.add_handler(CommandHandler("learn", learn))
                                                                                   dispatcher.add_handler(CommandHandler("revise", revise))
                                                                                   dispatcher.add_handler(CommandHandler("quote", quote))
                                                                                   dispatcher.add_handler(CommandHandler("more", more))
                                                                                   dispatcher.add_handler(CommandHandler("quiz", quiz))
                                                                                  
                                                                                   # on non command i.e message
                                                                                  
                                                                                   dispatcher.add_handler(MessageHandler(Filters.text, messageHandler))
                                                                                  
                                                                                   # Start the Bot
                                                                                   updater.start_polling(drop_pending_updates=True)
                                                                                  
                                                                                   updater.idle()
                                                                                  
                                                                                  Creating a function to concatenate strings based on len(array)
                                                                                  Pythondot imgLines of Code : 3dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                  copy iconCopy
                                                                                  list_of_strings_from_txt = ["A","B","C"]
                                                                                  modified_list = [f"{w}: {input(f'{w}:')}" for w in list_of_strings_from_txt]
                                                                                  
                                                                                  Creating a function to concatenate strings based on len(array)
                                                                                  Pythondot imgLines of Code : 37dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                  copy iconCopy
                                                                                  import csv
                                                                                  
                                                                                  with open('filename.csv', 'r') as fd:
                                                                                      reader = csv.reader(fd)
                                                                                      for row in reader:
                                                                                          # do something
                                                                                  
                                                                                  import csv
                                                                                  
                                                                                  actual_text_list = []
                                                                                  with open('filename.csv', 'r') as fd:
                                                                                      reader = csv.reader(fd)
                                                                                      for row in reader:
                                                                                          actual_text_list.append(row)
                                                                                  
                                                                                  user_input_list = []
                                                                                  for actual_text in actual_text_list:
                                                                                      the_users_input = input(f'What is your response to {actual_text}? ')    
                                                                                      user_input_list.append(the_users_input)
                                                                                  
                                                                                  import csv
                                                                                  
                                                                                  actual_text_list = []
                                                                                  with open('filename.csv', 'r') as fd:
                                                                                      reader = csv.reader(fd)
                                                                                      for row in reader:
                                                                                          actual_text_list.append(row)
                                                                                  
                                                                                  dictionary = dict()
                                                                                  for actual_text in actual_text_list:
                                                                                      the_users_input = input(f'What is your response to {actual_text}? ')    
                                                                                      dictionary[actual_text] = the_users_input
                                                                                  
                                                                                  for actual_text, user_input in dictionary.items():
                                                                                      print(f'In response to {actual_text}, you specified {user_input}.')
                                                                                  
                                                                                  
                                                                                  Unable to get data collector to display values (Keeps displaying None value)
                                                                                  Pythondot imgLines of Code : 35dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                  copy iconCopy
                                                                                      answer = "You have keyed in " + user_input + " calories! Click Back to go back to the main menu"
                                                                                      
                                                                                      return answer
                                                                                      
                                                                                  #Storing Data in a class 
                                                                                  
                                                                                  cal = []
                                                                                  
                                                                                  # calculates total cals
                                                                                  def cal_calculator(update: Update,_: CallbackContext):
                                                                                      query = update.callback_query
                                                                                      query.answer()
                                                                                      keyboard = [
                                                                                          [InlineKeyboardButton("Back", callback_data = str(ONE))]
                                                                                      ]
                                                                                      reply_markup = InlineKeyboardMarkup(keyboard)
                                                                                      
                                                                                      
                                                                                      
                                                                                      query.edit_message_text(f'You have consumed {str(sum(cal))}',reply_markup = reply_markup)
                                                                                      return SECOND
                                                                                  
                                                                                  
                                                                                  
                                                                                  def calorie_reply(update: Update,_ : CallbackContext):
                                                                                      keyboard = [
                                                                                          [InlineKeyboardButton("Back", callback_data = str(ONE))]
                                                                                      ]
                                                                                      reply_markup = InlineKeyboardMarkup(keyboard)
                                                                                      user_input = update.message.text 
                                                                                      user_int = int(user_input)
                                                                                      cal.append(user_int)
                                                                                      update.message.reply_text(calorie_input(user_input),reply_markup = reply_markup)
                                                                                      return SECOND
                                                                                  
                                                                                  How can I continue a nested conversation in a separate file
                                                                                  Pythondot imgLines of Code : 5dot imgLicense : Strong Copyleft (CC BY-SA 4.0)
                                                                                  copy iconCopy
                                                                                  import src.main from main
                                                                                  
                                                                                  if '__name__' == '__main__':
                                                                                      main()
                                                                                  
                                                                                  Community Discussions

                                                                                  Trending Discussions on python-telegram-bot

                                                                                  How to use Google Oauth2.0 to authenticate user via Telegram Bot
                                                                                  chevron right
                                                                                  Python Telegram Bot + 3rd Party Listener
                                                                                  chevron right
                                                                                  Python Telegram Bot Chat.ban_member() issues
                                                                                  chevron right
                                                                                  python-telegram-bot: Pass arguments to the bot
                                                                                  chevron right
                                                                                  How I can do a send message task inside a dispatcher.run_async() using python-telegram-bot?
                                                                                  chevron right
                                                                                  Telegram bot (Flask app on Google Cloud Run) stops responding after instance reloading
                                                                                  chevron right
                                                                                  How do make a telegram bot to read a pdf file sent by user and extract data from it?
                                                                                  chevron right
                                                                                  How can I continue a nested conversation in a separate file
                                                                                  chevron right
                                                                                  How to send Django model object's details as answer according to the user's choice of InlineKeyboardButton?
                                                                                  chevron right
                                                                                  Python Telegram Bot Read Large Message
                                                                                  chevron right

                                                                                  QUESTION

                                                                                  How to use Google Oauth2.0 to authenticate user via Telegram Bot
                                                                                  Asked 2022-Mar-30 at 08:56

                                                                                  This is my first time interacting with Google API and I'm using python3.9 with this library Python Telegram Bot I want to access a user Google API Calendar via a telegram bot and I can't seem to find any article to guide me through it. My key problem (I think) is redirecting the success authorization flow back to telegram bot.

                                                                                  This is what I have in mind:

                                                                                  1. In the telegram app, user send '/send' to bot
                                                                                  2. Bot receive message and return a google an authorization link to user
                                                                                  3. User clink on authorization link and allow access
                                                                                  4. Bot receive authorization access and completes the Oauth flow

                                                                                  The problem lies betweeen step 3 and 4. A standard authorization link is https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=&scope=&state&access_type=offline

                                                                                  How do I send the authorization link back to my telegram bot? Should I create another API endpoint to receive that authorization link? Or can I send telegram api send_message() in the to redirect the success message to my bot.

                                                                                  Update 1

                                                                                  Thanks to CallMeStag, I manage to figure out a way to complete the oauth process. For people who faced the same problem, this is what I did Pre-requisite: Credentials is created in google console api - Web application. redirect_uri set as localhost:8000 (During development phase)

                                                                                  1. User send '/send' to bot
                                                                                  2. Bot receive message and return an authorization link
                                                                                  https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=&redirect_uri=http://localhost:8000/&scope=&state&access_type=offline
                                                                                • User click on link to authenticate and it will redirect to http://localhost:8000. Used fastapi as a webhook to receive the message. Capture the authorization code, use google.oauthlib.flow to complete the authorization process. Next, redirect user back to telegram link https://t.me/
                                                                                • Start using user google calendar
                                                                                • ANSWER

                                                                                  Answered 2022-Mar-29 at 06:44

                                                                                  It's currently indeed not very straight forward for a PTB-application to listen for external updates (the auth verification in this cas) - see also this issue. Currently it might be easiest for you to set up a custom webhook application that runs in parallel to the Updater - e.g. using flask/django/starlette/fastapi/…. Alternatively, if you're using webhooks for your bot anyway, you can patch the Updater to do the job for you. Although that requires some manual work - see here for an example.

                                                                                  Once you are able to listen to updates coming from Google, handling them can be done via PTBs usual handler setup, specifically via the TypeHandler or even a custom Handler subclass - see this FAQ entry.

                                                                                  Regarding the redirect url: You'll want to redirect your user back to your bot, so you'll have to provide a link that does that. Bot.link should probably do the trick.

                                                                                  Disclaimer: I'm currently the maintainer of python-telegram-bot.

                                                                                  Source https://stackoverflow.com/questions/71651533

                                                                                  QUESTION

                                                                                  Python Telegram Bot + 3rd Party Listener
                                                                                  Asked 2022-Mar-29 at 10:26

                                                                                  I'm working with Python Telegram Bot https://python-telegram-bot.readthedocs.io/

                                                                                  I'm trying to implement a bot function into a 3rd party listener, so when the handle_event() is fired, with a telegram username as parameter, the bot bans that member from the group. here we go with an example:

                                                                                  def handle_event(event):
                                                                                  
                                                                                          result = offerCancelled_event.processReceipt(receipt) 
                                                                                  
                                                                                          #Double check if we got a result
                                                                                          if(result):
                                                                                  
                                                                                              #Set telegram user
                                                                                              telegram_user = "Ulvur"
                                                                                  
                                                                                              users.remove(telegram_user)
                                                                                              file = open('users.txt', 'w+')
                                                                                              for user in users:
                                                                                                  file.write('%s\n' % user)
                                                                                                  file.close()
                                                                                  
                                                                                              if(telegram_user not in users):
                                                                                                  Update.effective_chat.ban_member(telegram_user)
                                                                                  

                                                                                  Following code is returning AttributeError: 'property' object has no attribute 'ban_member' when function is fired.

                                                                                  ANSWER

                                                                                  Answered 2022-Mar-29 at 10:26

                                                                                  Update is a class and the property Update.effective_chat can only be properly evaluated for instances of that class. If you want to make simple calls to the Bot API, you should instantiate an instance of the telegram.Bot class and call its methods - in your case Bot.ban_chat_member. Please see the Introduction to the API for a more detailed explanation of how to use the API methods.

                                                                                  Source https://stackoverflow.com/questions/71660040

                                                                                  QUESTION

                                                                                  Python Telegram Bot Chat.ban_member() issues
                                                                                  Asked 2022-Mar-21 at 17:05

                                                                                  I'm working with Python Telegram Bot https://python-telegram-bot.readthedocs.io/en/stable/telegram.chat.html and trying to build my first bot on telegram.

                                                                                  I've followed the example https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/chatmemberbot.py as a template

                                                                                  I want to add a functionality that if a user is not in the list, the bot should kick him out but I'm having some issues implementing this function. My code is as follows:

                                                                                  #!/usr/bin/env python
                                                                                  # pylint: disable=C0116,W0613
                                                                                  # This program is dedicated to the public domain under the CC0 license.
                                                                                  
                                                                                  """
                                                                                  Simple Bot to handle '(my_)chat_member' updates.
                                                                                  Greets new users & keeps track of which chats the bot is in.
                                                                                  Usage:
                                                                                  Press Ctrl-C on the command line or send a signal to the process to stop the
                                                                                  bot.
                                                                                  """
                                                                                  
                                                                                  import logging
                                                                                  from typing import Tuple, Optional
                                                                                  
                                                                                  from telegram import Update, Chat, ChatMember, ParseMode, ChatMemberUpdated
                                                                                  from telegram.ext import (
                                                                                      Updater,
                                                                                      CommandHandler,
                                                                                      CallbackContext,
                                                                                      ChatMemberHandler,
                                                                                  )
                                                                                  
                                                                                  # Enable logging
                                                                                  logging.basicConfig(
                                                                                      format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
                                                                                  )
                                                                                  
                                                                                  logger = logging.getLogger(__name__)
                                                                                  
                                                                                  def checkUsers(update: Update, context: CallbackContext, chat: Chat) -> None:
                                                                                      """Greets new users in chats and announces when someone leaves"""
                                                                                  
                                                                                      cause_name = update.chat_member.from_user.mention_html()
                                                                                      member_name = update.chat_member.new_chat_member.user.mention_html()
                                                                                      
                                                                                      member = update.chat_member.new_chat_member.user.username
                                                                                      userId = update.chat_member.new_chat_member.user.id
                                                                                      print(userId)
                                                                                      
                                                                                      approvedMembers = ["Jack", "shaamsCat"]
                                                                                      
                                                                                      if member in approvedMembers :
                                                                                          update.effective_chat.send_message(
                                                                                              f"{member_name} was added by {cause_name}. Welcome!",
                                                                                              parse_mode=ParseMode.HTML,
                                                                                          )
                                                                                      elif member not in approvedMembers : 
                                                                                          update.effective_chat.send_message(
                                                                                              f"{member_name} is not on the list!",
                                                                                              parse_mode=ParseMode.HTML,
                                                                                          ),
                                                                                                  
                                                                                      chat.ban_member(userId)
                                                                                         
                                                                                  
                                                                                  def main() -> None:
                                                                                      """Start the bot."""
                                                                                      # Create the Updater and pass it your bot's token.
                                                                                      updater = Updater("TOKEN")
                                                                                  
                                                                                      # Get the dispatcher to register handlers
                                                                                      dispatcher = updater.dispatcher
                                                                                  
                                                                                      # Handle members joining/leaving chats.
                                                                                      dispatcher.add_handler(ChatMemberHandler(checkUsers, ChatMemberHandler.CHAT_MEMBER))
                                                                                  
                                                                                      # Start the Bot
                                                                                      # We pass 'allowed_updates' handle *all* updates including `chat_member` updates
                                                                                      # To reset this, simply pass `allowed_updates=[]`
                                                                                      updater.start_polling(allowed_updates=Update.ALL_TYPES)
                                                                                  
                                                                                      # 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()
                                                                                  

                                                                                  And I receive the following error in that case:

                                                                                  TypeError: checkUsers() missing 1 required positional argument: 'chat'
                                                                                  

                                                                                  If I change the function checkUsers() function to look like this:

                                                                                  def checkUsers(update: Update, context: CallbackContext) -> None:
                                                                                  """Greets new users in chats and announces when someone leaves"""
                                                                                  
                                                                                  cause_name = update.chat_member.from_user.mention_html()
                                                                                  member_name = update.chat_member.new_chat_member.user.mention_html()
                                                                                  
                                                                                  member = update.chat_member.new_chat_member.user.username
                                                                                  userId = update.chat_member.new_chat_member.user.id
                                                                                  print(userId)
                                                                                  
                                                                                  approvedMembers = ["Jack", "shaamsCat"]
                                                                                  
                                                                                  if member in approvedMembers :
                                                                                      update.effective_chat.send_message(
                                                                                          f"{member_name} was added by {cause_name}. Welcome!",
                                                                                          parse_mode=ParseMode.HTML,
                                                                                      )
                                                                                  elif member not in approvedMembers : 
                                                                                      update.effective_chat.send_message(
                                                                                          f"{member_name} is not on the list!",
                                                                                          parse_mode=ParseMode.HTML,
                                                                                      ),
                                                                                              
                                                                                  Chat.ban_member(userId)
                                                                                     
                                                                                  

                                                                                  Then the error is:

                                                                                  TypeError: ban_member() missing 1 required positional argument: 'user_id'
                                                                                  

                                                                                  And if I pass no arguments to Chat.ban_member() then the arguments missing looks like follows:

                                                                                  TypeError: ban_member() missing 2 required positional arguments: 'self' and 'user_id'
                                                                                  

                                                                                  I'll appreaciate any help, I'm sure it's going to be any fundamental knowledge I'm missing, I will be honest and tell you I did just start working with Python some days ago, so be nice please!!

                                                                                  Thank you!

                                                                                  ANSWER

                                                                                  Answered 2022-Mar-21 at 17:05

                                                                                  Handler callbacks must have exactly two positional arguments - that's just how python-telegram-bot is designed. That's why your first approach doesn't work.

                                                                                  Moreover, Chat.ban_member is a bounded method, not a class/static method. Chat is a class, not an instance of that class, so Chat.ban_member(user_id) can't work either. You need an instance of the Chat class to call that method. In your case probably update.chat_member.chat or update.effective_chat (the latter being a shortcut for the former).

                                                                                  Disclaimer: I'm currently the maintainer of python-telegram-bot.

                                                                                  Source https://stackoverflow.com/questions/71549717

                                                                                  QUESTION

                                                                                  python-telegram-bot: Pass arguments to the bot
                                                                                  Asked 2022-Mar-21 at 04:36

                                                                                  I see some bots acts regarding args passed to the bot. For example:

                                                                                  https://t.me/name_bot?start=something
                                                                                  

                                                                                  This is helpful for sending invitations/ referral / promo codes etc.

                                                                                  How can I handle this using python-telegram-bot and getting the start value?

                                                                                  Is it possible to use different vars other than start?

                                                                                  ANSWER

                                                                                  Answered 2022-Mar-21 at 04:36

                                                                                  I never tested it before but it seems you have it in documentation as deep linking.

                                                                                  start=something should run command /start something

                                                                                  Link can use only start or startgroup

                                                                                  import os
                                                                                  import telegram
                                                                                  from telegram.ext import Updater, CommandHandler
                                                                                  
                                                                                  # --- init ---
                                                                                  
                                                                                  TOKEN = os.getenv('TELEGRAM_TOKEN')
                                                                                  
                                                                                  updater = Updater(token=TOKEN, use_context=True)
                                                                                  dispatcher = updater.dispatcher
                                                                                  
                                                                                  # --- commands ---
                                                                                  
                                                                                  def start(update, context):
                                                                                      print('text:', update.message.text)   # /start something
                                                                                      print('args:', context.args)          # ['something']
                                                                                  
                                                                                  dispatcher.add_handler(CommandHandler('start', start))
                                                                                  
                                                                                  # --- start ---
                                                                                      
                                                                                  updater.start_polling()
                                                                                  updater.idle()
                                                                                  

                                                                                  Source https://stackoverflow.com/questions/71551866

                                                                                  QUESTION

                                                                                  How I can do a send message task inside a dispatcher.run_async() using python-telegram-bot?
                                                                                  Asked 2022-Mar-09 at 11:00

                                                                                  I develop a telegram bot using the python-telegram-bot module. I attempt to run a function without executing it using dispatcher.run_async(myfunction) but how I can do a task for example sending a message from inside the dispatcher.run_async()?

                                                                                  I have all the users id in my database. And this is the snippet of my code.

                                                                                  import telegram
                                                                                  from telegram.ext import Updater
                                                                                  
                                                                                  def myfunction():
                                                                                      # bot.send_message(text='Hello, World',chat_id=)
                                                                                  
                                                                                  def main():
                                                                                      """Start the bot."""
                                                                                      # Create the Updater and pass it your bot's token.
                                                                                      updater = Updater("TOKEN")
                                                                                  
                                                                                      # Get the dispatcher to register handlers
                                                                                      dispatcher = updater.dispatcher
                                                                                      dispatcher.run_async(myfunction)
                                                                                  
                                                                                      # Start the Bot
                                                                                      updater.start_polling()
                                                                                      updater.idle()
                                                                                  
                                                                                  if __name__ == '__main__':
                                                                                      main()
                                                                                  

                                                                                  ANSWER

                                                                                  Answered 2022-Mar-09 at 11:00

                                                                                  Not sure if this is the intended way, but you can pass the bot the the function by passing it to run_async:

                                                                                  dispatcher.run_async(myFunction, updater.bot)
                                                                                  
                                                                                  def myfunction(bot):
                                                                                      bot.send_message(text='Hello, World',chat_id=123456789)
                                                                                  
                                                                                  import telegram
                                                                                  from telegram.ext import Updater
                                                                                  
                                                                                  def myfunction(bot):
                                                                                      bot.send_message(text='Hello, World',chat_id=123456789)
                                                                                  
                                                                                  def main():
                                                                                      """Start the bot."""
                                                                                      # Create the Updater and pass it your bot's token.
                                                                                      updater = Updater("")
                                                                                  
                                                                                      # Get the dispatcher to register handlers
                                                                                      dispatcher = updater.dispatcher
                                                                                      dispatcher.run_async(myfunction, updater.bot)
                                                                                  
                                                                                      # Start the Bot
                                                                                      updater.start_polling()
                                                                                      updater.idle()
                                                                                  
                                                                                  if __name__ == '__main__':
                                                                                      main()
                                                                                  

                                                                                  Source https://stackoverflow.com/questions/71408020

                                                                                  QUESTION

                                                                                  Telegram bot (Flask app on Google Cloud Run) stops responding after instance reloading
                                                                                  Asked 2022-Feb-24 at 07:51

                                                                                  I'm trying to make my first Telegram bot on Python. I use the python-telegram-bot, Flask, run it in Google Cloud Run. Locally on my machine everything works fine, when I deploy it (using Docker) to Google Cloud Run everything also works fine until the moment when Google Cloud Run stops the instance. That's what I see in Gloud Run logs:

                                                                                  [2022-02-23 11:09:24 +0000] [1] [INFO] Handling signal: term
                                                                                  [2022-02-23 11:09:24 +0000] [3] [INFO] Worker exiting (pid: 3)
                                                                                  [2022-02-23 11:09:25 +0000] [1] [INFO] Shutting down: Master
                                                                                  

                                                                                  After this bot stops responding.

                                                                                  I tried to set up a min inctances feature (set a min-instances=1) but it didn't help, bot stops responding after a while.

                                                                                  This is a part of code from main.py:

                                                                                  ...
                                                                                  @app.route("/")
                                                                                  def main() -> None:
                                                                                      updater = Updater("TOKEN")
                                                                                      dispatcher = updater.dispatcher
                                                                                      conv_handler = ConversationHandler(
                                                                                          entry_points=[CommandHandler('start', start), CommandHandler('menu', menu), CallbackQueryHandler(button)],
                                                                                          states={
                                                                                              START: [
                                                                                                  MessageHandler(Filters.regex('^(Default Text)$') & (~ Filters.command), start)
                                                                                              ]},
                                                                                          fallbacks=[CommandHandler('cancel', cancel), CommandHandler('menu', menu)],
                                                                                          allow_reentry=True
                                                                                      )
                                                                                  
                                                                                      dispatcher.add_handler(conv_handler)
                                                                                      updater.start_polling()
                                                                                      updater.idle()
                                                                                  
                                                                                  
                                                                                  if __name__ == "__main__":
                                                                                      app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))
                                                                                  

                                                                                  I guess the problem is that the bot starts only when I make GET request to my Google Cloud URL: app.route("/") is triggered and the main function is started. If the Cloud Run instance stops and I manually request my service URL (that I get from Google Cloud Run, like https://service-name-xxxxx-xx.a.run.app), the bot starts up again. This is a logs from Cloud Run after this:

                                                                                  [2022-02-23 11:09:24 +0000] [1] [INFO] Handling signal: term
                                                                                  [2022-02-23 11:09:24 +0000] [3] [INFO] Worker exiting (pid: 3)
                                                                                  [2022-02-23 11:09:25 +0000] [1] [INFO] Shutting down: Master
                                                                                  

                                                                                  ---I manualy requested a service url---

                                                                                  [2022-02-23 19:03:21 +0000] [1] [INFO] Starting gunicorn 20.1.0
                                                                                  [2022-02-23 19:03:21 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
                                                                                  [2022-02-23 19:03:21 +0000] [1] [INFO] Using worker: gthread
                                                                                  [2022-02-23 19:03:21 +0000] [3] [INFO] Booting worker with pid: 3
                                                                                  2022-02-23 19:03:21,985 - apscheduler.scheduler - INFO - Scheduler started
                                                                                  

                                                                                  Honestly, I couldn't think of anything better than to keep track of instance restarts and request the URL every time.

                                                                                  I hope for your advice, thank you!

                                                                                  ANSWER

                                                                                  Answered 2022-Feb-24 at 07:51

                                                                                  TBF, I'm not familiar with Google Cloud Run, but if I understand correctly the point is that you code will only be invoked when a request is made to the app, i.e. it's one of those "serverless" setups - is that correct?

                                                                                  If so: What updater.start_polling() does is start a long running background thread that fetches updates continuously. To have your bot responsive 24/7 with this method, your script needs to run 24/7. Now the point of serverless setups is that your code only runs on demand, so for this hosting method a more reasonable approach would be to only invoke your code when your bot receives an update. This can achieved using a webhook instead of long polling. There is a section on this in the PTB wiki. See also this thread about AWS Lambda, which is similar AFAIK.

                                                                                  However one should note that stateful logic like ConversationHandler is hard to realize in such setups: By default ConversationHandler keeps track of the current state in memory, so the information is lost on shutdown of the process. You can use persistence to store the data, but I'm not sure how well this works with serverless setups - there might be race conditions if multiple updates come in at the same time. So another idea would be to switch to a different hosting service that allows to run your process 24/7.

                                                                                  Disclaimer: I'm currently the maintainer of python-telegram-bot.

                                                                                  Source https://stackoverflow.com/questions/71243015

                                                                                  QUESTION

                                                                                  How do make a telegram bot to read a pdf file sent by user and extract data from it?
                                                                                  Asked 2022-Feb-23 at 14:59

                                                                                  I have been checking on forums(stack overflow, git, Telegram APIs) to check how could I extract contents from a pdf file which is sent by user? I have created Telegram bot using python's python-telegram-bot library and as an efforts to try to solve my question, I had checked links eg: https://pypi.org/project/python-telegram-bot/ and https://python-telegram-bot.readthedocs.io/en/stable/index.html in search of functions but couldn't find anything there.

                                                                                  I did find methods to send a pdf file to user from bot and send a file from user to bot but there is nothing available where BOT can extract contents of pdf sent by user. Will be grateful if experts could guide me here.

                                                                                  ANSWER

                                                                                  Answered 2022-Feb-23 at 14:59

                                                                                  python-telegram-bot is a library that provides a wrapper for the Telegram Bot API. As such, it provides all the methods from the API as well as auxiliary functionality to build chat bots in general, including downloading files sent by users. Extracting contents from a received PDF file after download is however far beyond the scope of this library.

                                                                                  Of course there are other libraries that provide such functionality and that can be used in combinantion with python-telgram-bot. See e.g. camelot.

                                                                                  Disclaimer: I'm currently the maintainer of python-telegram-bot.

                                                                                  Source https://stackoverflow.com/questions/71237863

                                                                                  QUESTION

                                                                                  How can I continue a nested conversation in a separate file
                                                                                  Asked 2022-Feb-17 at 00:39

                                                                                  I am not a professional programmer but I'm trying to build a python-telegram-bot for work using ConversationHandlers. Basically, I offer users a menu of options, summarized as:

                                                                                  • Complete Survey
                                                                                  • EXIT

                                                                                  If "Complete Survey" is selected, the bot then asks for the user ID. Depending on the user ID I assign the user 1 of 30+ different surveys (I'm trying to use child conversations). Over time this list of surveys will grow and each survey has unique questions and steps to it.

                                                                                  Given the number of surveys, I thought of managing each survey as a child conversation with its own ConversationHandler, and running it from a separate file/module (to keep things dynamic and not have one HUGE file with n+ variables to consider).

                                                                                  The thing is, how can I continue the child conversation from a separate file? Is there another way to approach this? I understand that the bot is still running from the main file and checking for updates. I would like to run each survey and, once finished, return to the INITIAL bot menu (parent conversation).

                                                                                  I found this previous discussion but my knowledge barely goes beyond the python-telegram-bot examples so I'm having a hard time following along: https://github.com/python-telegram-bot/python-telegram-bot/issues/2388

                                                                                  Here is an example summarized code of what I'm trying to do:

                                                                                  main_file.py

                                                                                  from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, InlineKeyboardMarkup, InlineKeyboardButton, Update, KeyboardButton, Bot, InputMediaPhoto
                                                                                  from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler, CallbackContext
                                                                                  import surveys  # this file contains each survey as a function with its own ConversationHandler
                                                                                  
                                                                                  token = ''
                                                                                  
                                                                                  MENU, USER, CHAT_ID, USER_ID, FINISHED = map(chr, range(1,6))
                                                                                  
                                                                                  END = ConversationHandler.END
                                                                                  
                                                                                  
                                                                                  def start(update: Update, context: CallbackContext) -> int:
                                                                                      """Initialize the bot"""
                                                                                  
                                                                                      context.user_data[CHAT_ID] = update.message.chat_id
                                                                                      text = 'Select an option:'
                                                                                      reply_keyboard = [
                                                                                          ['Complete Survey'],
                                                                                          ['EXIT'],
                                                                                      ]
                                                                                  
                                                                                      context.bot.send_message(
                                                                                          context.user_data[CHAT_ID],
                                                                                          text=text,
                                                                                          reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
                                                                                      )
                                                                                  
                                                                                      return MENU
                                                                                  
                                                                                  
                                                                                  def exit(update:Update, context:CallbackContext) -> None:
                                                                                      """Exit from the main menu"""
                                                                                      context.bot.send_message(
                                                                                          context.user_data[CHAT_ID],
                                                                                          text='OK bye!',
                                                                                          reply_markup=ReplyKeyboardRemove()
                                                                                      )
                                                                                      return END
                                                                                  
                                                                                  
                                                                                  def abrupt_exit(update:Update, context:CallbackContext) -> None:
                                                                                      """Exit the main conversation to enter the survey conversation"""
                                                                                  
                                                                                      return END
                                                                                  
                                                                                  
                                                                                  def survey_start(update:Update, context:CallbackContext) -> None:
                                                                                      """Asks for the user_id in order to determine which survey to offer"""
                                                                                      text = 'Please type in your company ID'
                                                                                      context.bot.send_message(
                                                                                          context.user_data[CHAT_ID],
                                                                                          text=text,
                                                                                          reply_markup=ReplyKeyboardRemove()
                                                                                      )
                                                                                      return USER
                                                                                  
                                                                                  
                                                                                  def survey_select(update:Update, context:CallbackContext) -> None:
                                                                                      """Search database to find next survey to complete"""
                                                                                      user = str(update.message.text)
                                                                                      chat_id = context.user_data[CHAT_ID]
                                                                                      context.user_data[USER_ID] = user
                                                                                  
                                                                                      """Search database with user_id and return survey to complete"""
                                                                                      survey = 'survey_a'     # this value is obtained from the database
                                                                                  
                                                                                      runSurvey = getattr(surveys, survey)   # I used getattr to load functions in a different module
                                                                                      runSurvey(Update, CallbackContext, user, chat_id, token)
                                                                                  
                                                                                      return FINISHED
                                                                                  
                                                                                  
                                                                                  def main() -> None:
                                                                                      updater = Updater(token, use_context=True)
                                                                                  
                                                                                      # Get the dispatcher to register handlers
                                                                                      dispatcher = updater.dispatcher
                                                                                  
                                                                                      # Survey conversation
                                                                                      survey_handler = ConversationHandler(
                                                                                          entry_points=[
                                                                                              MessageHandler(Filters.regex('^Complete Survey$'), survey_start),
                                                                                          ],
                                                                                  
                                                                                          states={
                                                                                              USER: [
                                                                                                  MessageHandler(Filters.text, survey_select),
                                                                                              ],
                                                                                              FINISHED: [
                                                                                                  # I'm guessing here I should add something to exit the survey ConversationHandler
                                                                                              ],
                                                                                          },
                                                                                          fallbacks=[
                                                                                              CommandHandler('stop', exit),
                                                                                          ],
                                                                                      )
                                                                                  
                                                                                  
                                                                                          # Initial conversation
                                                                                      conv_handler=ConversationHandler(
                                                                                          entry_points=[
                                                                                              CommandHandler('start', start),
                                                                                          ],
                                                                                          states={
                                                                                              MENU: [
                                                                                                  MessageHandler(Filters.regex('^Complete Survey$'), abrupt_exit),
                                                                                                  MessageHandler(Filters.regex('^EXIT$'), exit),
                                                                                              ],
                                                                                          },
                                                                                          allow_reentry=True,
                                                                                          fallbacks=[
                                                                                              CommandHandler('stop', exit),
                                                                                          ],
                                                                                      )
                                                                                  
                                                                                      dispatcher.add_handler(conv_handler, group=0)  # I used separate groups because I tried ending
                                                                                      dispatcher.add_handler(survey_handler, group=1)  # the initial conversation and starting the other
                                                                                  
                                                                                      # Start the Bot
                                                                                      updater.start_polling()
                                                                                      updater.idle()
                                                                                  
                                                                                  if __name__ == '__main__':
                                                                                      main()
                                                                                  

                                                                                  surveys.py This is where each survey is with its own conversation and functions to call. Basically I enter survey_A (previously selected) and am trying to use it as the main()

                                                                                  from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, InlineKeyboardMarkup, InlineKeyboardButton, Update, \
                                                                                      KeyboardButton, Bot, InputMediaPhoto
                                                                                  from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler, CallbackQueryHandler, \
                                                                                      CallbackContext
                                                                                  
                                                                                  NEXT_QUESTION, LAST_QUESTION, CHAT_ID = map(chr, range(1,4))
                                                                                  
                                                                                  END = ConversationHandler.END
                                                                                  
                                                                                  def exit(update:Update, context:CallbackContext) -> None:
                                                                                      """Exit from the main menu"""
                                                                                      context.bot.send_message(
                                                                                          context.user_data[CHAT_ID],
                                                                                          text='OK bye!',
                                                                                          reply_markup=ReplyKeyboardRemove()
                                                                                      )
                                                                                      return END
                                                                                  
                                                                                  
                                                                                  def first_q(update:Update, context:CallbackContext, chat_id:str) -> None:
                                                                                      """First survey_A question"""
                                                                                  
                                                                                      context.bot.send_message(
                                                                                          chat_id,
                                                                                          text='What is your name?',
                                                                                          reply_markup=ReplyKeyboardRemove()
                                                                                      )
                                                                                      return NEXT_QUESTION
                                                                                  
                                                                                  
                                                                                  def last_q(update: Update, context: CallbackContext) -> None:
                                                                                      """Last survey_A question"""
                                                                                  
                                                                                      update.message.reply_text(
                                                                                          'How old are you?', reply_markup=ReplyKeyboardRemove()
                                                                                      )
                                                                                      return LAST_QUESTION
                                                                                  
                                                                                  def survey_a(update:Update, context:CallbackContext, user:str, chat_id: str, token:str) -> None:
                                                                                      """This function acts like the main() for the survey A conversation"""
                                                                                      print(f'{user} will now respond survey_a')
                                                                                      CHAT_ID = chat_id   # identify the chat_id to use
                                                                                      updater = Updater(token, use_context=True)   # here I thought of calling the Updater once more
                                                                                  
                                                                                      survey_a_handler = ConversationHandler(
                                                                                          entry_points=[
                                                                                              MessageHandler(Filters.text, first_q),
                                                                                          ],
                                                                                          states={
                                                                                              NEXT_QUESTION: [
                                                                                                  MessageHandler(Filters.text, last_q),
                                                                                              ],
                                                                                              LAST_QUESTION: [
                                                                                                  MessageHandler(Filters.text, exit),
                                                                                              ],
                                                                                  
                                                                                          },
                                                                                          allow_reentry=True,
                                                                                          fallbacks=[
                                                                                              CommandHandler('stop', exit),
                                                                                          ],
                                                                                      )
                                                                                  
                                                                                      updater.dispatcher.add_handler(survey_a_handler, group=0)  # I only want to add the corresponding
                                                                                                                                                 # survey conversation handler
                                                                                  
                                                                                      first_q(Update, CallbackContext, CHAT_ID)
                                                                                  

                                                                                  I run the code and it breaks at surveys.py line 23, in first_q: context.bot.send_message( AttributeError: 'property' object has no attribute 'send_message'

                                                                                  I assume my logic with the conversation handler is way off.

                                                                                  I appreciate any help

                                                                                  ANSWER

                                                                                  Answered 2022-Feb-17 at 00:39

                                                                                  I have been developing telegram bots for about a year now, and I hope the best approach is to structure your project first. Let me explain that all in detail.

                                                                                  "Foldering"

                                                                                  Folder structure

                                                                                  Basically, all the code is in the src folder of the project. Inside the src folder there is another sub-folder called components which includes all the different sections of your bot you want to work on (i.e your quiz_1, quiz_2, ...) and main.py file which includes the 'core' of the bot. However in the root directory of the project (which is just your project folder) you can see bot.py file which serves just as a runner file. So nothing more in there except just:

                                                                                  import src.main from main
                                                                                  
                                                                                  if '__name__' == '__main__':
                                                                                      main()
                                                                                  
                                                                                  Tips

                                                                                  So regarding your questionnaire:

                                                                                  • I would recommend using just strings as keys for the states instead of mapping them to random values. Basically you can do just like "MAIN_MENU", "STATE_ONE" , "STATE_TWO" and so on, but be sure to return the same string in the callback function!
                                                                                  • The overall logic of the PTB library is like: Telegram API server -> PTB Updater() class -> Dispatcher() (which is updater.dispatcher in your code) -> Handlers -> callback function -> <- user. The reason arrows point to user and back to callback function is because there is an interaction of your bot's logic and user, so that user's response goes back to your callback function code.
                                                                                  • I recommend not choosing callback function names as like 'first_question' or 'second_question'. Instead name it like get_question() use that function to retrieve question data from other source so that it can be dynamic. So for example, you will have a dictionary of different questions with keys of question number - simple, right? And then you will write a function that will send user a question according to its state and picking the right question with the right key from the dictionary. By this you can add more questions to your dictionary and no need to change the code in the function because it will be dynamic (as long as you write the correct function that will work).
                                                                                  • In your main.py file have only one main() function which will hold the Updater() with the given token, because you cannot have more than one Updater() with the same token. It's like one bot can be accessed by only and only one app that is polling at a time. Polling - visit here.
                                                                                  🎉 Great news!

                                                                                  To support your bot development and follow the structured project creation, I have created a repo on GitHub that holds almost the same project structure as I tried to explain to you today. Feel free to check it out, clone it and play around. Just add your token to .env file and run the bot.

                                                                                  More resources

                                                                                  Check out these projects as well:

                                                                                  As you will see in there main.py contains all the handlers and src folder contains all the different 'components' which are more of a like different parts of the bot.

                                                                                  If you need any help, I am here and more than happy to answer any of your questions.

                                                                                  Source https://stackoverflow.com/questions/71056125

                                                                                  QUESTION

                                                                                  How to send Django model object's details as answer according to the user's choice of InlineKeyboardButton?
                                                                                  Asked 2022-Feb-11 at 11:23

                                                                                  I'm new to creating telegram bots with Django. I selected python-telegram-bot library for creating bots with Django.

                                                                                  I've created a Django model, named as category:

                                                                                  class category(models.Model):
                                                                                      name = models.CharField(max_length=250)
                                                                                      category_id = models.IntegerField()
                                                                                  
                                                                                      def __str__(self):
                                                                                          return self.name
                                                                                  

                                                                                  and product:

                                                                                  class product(models.Model):
                                                                                      product_category = models.ForeignKey(category, on_delete=models.SET_NULL, blank=True, null=True)
                                                                                      name = models.CharField(max_length=250)
                                                                                      cost = models.FloatField()
                                                                                      img = models.ImageField()
                                                                                  
                                                                                      def __str__(self):
                                                                                          return self.name
                                                                                  

                                                                                  Successfully created InlineKeyboardButtons and placed id of each product model object to each button, with these functions:

                                                                                  def product(update: Update, context: CallbackContext):
                                                                                      query = update.callback_query.data
                                                                                  
                                                                                      product_button_list = []
                                                                                      
                                                                                      for each in product.objects.select_related().filter(product_category_id = query):
                                                                                          product_button_list.append(InlineKeyboardButton(each.name, callback_data=each.id))
                                                                                      reply_markup = InlineKeyboardMarkup(build_menu(product_button_list,n_cols=2))
                                                                                      update.callback_query.message.edit_text("Mahsulotni tanlang!", 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
                                                                                  

                                                                                  When the user selects a button named as the name of the product model object, I am receiving the correct id of the product model object, tested with printing the query:

                                                                                  def product_info(update: Update, context: CallbackContext):
                                                                                      query = update.callback_query.data
                                                                                      print(query)
                                                                                  
                                                                                      obj = product.objects.filter(pk=query)
                                                                                      print(obj)
                                                                                  

                                                                                  Question: How to reply Django product model object's fields including its image to the user according to the user's chosen product?

                                                                                  Like:

                                                                                  You have chosen a product:

                                                                                  Image of the product

                                                                                  Name of the product

                                                                                  Cost of the product

                                                                                  ANSWER

                                                                                  Answered 2022-Feb-11 at 11:23

                                                                                  Here is the answer, I have successed so far:

                                                                                  filename = 'path_until_media_folder' + str(obj.img.url) 
                                                                                  
                                                                                  update.callback_query.bot.send_photo(update.effective_chat.id, photo=open(filename, 'rb'))
                                                                                  

                                                                                  Source https://stackoverflow.com/questions/70693182

                                                                                  QUESTION

                                                                                  Python Telegram Bot Read Large Message
                                                                                  Asked 2022-Feb-02 at 10:00

                                                                                  I am trying to create a Telegram bot using python-telegram-bot that will read in a large message (~12K characters) and save the output to a file.

                                                                                  The relevant code I have is:

                                                                                  updater = Updater(token='XXXXXXXXXXXXXXXXXX')
                                                                                  dispatcher = updater.dispatcher
                                                                                  
                                                                                  def runMe(update, context):
                                                                                      received_message = update.message.text
                                                                                      print("Received message with length %d" % len(received_message))
                                                                                      # Save (full) message to file
                                                                                  
                                                                                  arg_handler = MessageHandler(Filters.text, runMe)
                                                                                  dispatcher.add_handler(arg_handler)
                                                                                  

                                                                                  However when I do this, Telegram splits the messages into chunks of 4,096 (max message size)

                                                                                  Received message with length 4096

                                                                                  Received message with length 4096

                                                                                  Received message with length 4095 (Not sure why this is 4095)

                                                                                  Received message with length 24

                                                                                  How do I modify the bot so that although the messages are sent in chunks, I am able to combine all of these to create the single original message with no modification to the formatting.

                                                                                  My original idea was to create a global variable and append each message chunk to a list but I am not sure how to achieve this short of sending a command like /startMessage and /endMessage to signify when the message is sent. This method seems inefficient.

                                                                                  ANSWER

                                                                                  Answered 2022-Feb-02 at 10:00

                                                                                  an idea could be to use a global variable and append all chunck to that, using message_id to understand when message is different.

                                                                                  updater = Updater(token='XXXXXXXXXXXXXXXXXX')
                                                                                  dispatcher = updater.dispatcher
                                                                                  
                                                                                  all_message = {
                                                                                      message_id = 0
                                                                                      text = ""
                                                                                  }
                                                                                  
                                                                                  def runMe(update, context):
                                                                                      global all_message
                                                                                      if update.message.message_id == all_message["message_id"]:
                                                                                          received_message = update.message.text
                                                                                          all_message["text"] += received_message 
                                                                                          print("Received message with length %d" % len(received_message))
                                                                                          # Save (full) message to file
                                                                                      else:
                                                                                          all_message["message_id"] = update.message.message_id
                                                                                          all_message["text"] += received_message
                                                                                  
                                                                                  arg_handler = MessageHandler(Filters.text, runMe)
                                                                                  dispatcher.add_handler(arg_handler)
                                                                                  

                                                                                  The problem could be when you consider the message ended for re-send, store or process it. Is not a great solution but you could consider the previous message ended when you go in the else condition, or you can check if chuck is <4095-4096, or again add a special character at the end of the message

                                                                                  Source https://stackoverflow.com/questions/70933845

                                                                                  Community Discussions, Code Snippets contain sources that include Stack Exchange Network

                                                                                  Vulnerabilities

                                                                                  No vulnerabilities reported

                                                                                  Install python-telegram-bot

                                                                                  You can install using 'pip install python-telegram-bot' or download it from GitHub, PyPI.
                                                                                  You can use python-telegram-bot like any standard Python library. You will need to make sure that you have a development environment consisting of a Python distribution including header files, a compiler, pip, and git installed. Make sure that your pip, setuptools, and wheel are up to date. When using pip it is generally recommended to install packages in a virtual environment to avoid changes to the system.

                                                                                  Support

                                                                                  For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .
                                                                                  Find more information at:
                                                                                  Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
                                                                                  Find more libraries
                                                                                  Explore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits​
                                                                                  Save this library and start creating your kit
                                                                                  Install
                                                                                • PyPI

                                                                                  pip install python-telegram-bot

                                                                                • CLONE
                                                                                • HTTPS

                                                                                  https://github.com/python-telegram-bot/python-telegram-bot.git

                                                                                • CLI

                                                                                  gh repo clone python-telegram-bot/python-telegram-bot

                                                                                • sshUrl

                                                                                  git@github.com:python-telegram-bot/python-telegram-bot.git

                                                                                • Share this Page

                                                                                  share link

                                                                                  Explore Related Topics

                                                                                  Consider Popular Chat Libraries

                                                                                  uni-app

                                                                                  by dcloudio

                                                                                  taro

                                                                                  by NervJS

                                                                                  ItChat

                                                                                  by littlecodersh

                                                                                  python-telegram-bot

                                                                                  by python-telegram-bot

                                                                                  tinker

                                                                                  by Tencent

                                                                                  Try Top Libraries by python-telegram-bot

                                                                                  rules-bot

                                                                                  by python-telegram-botPython

                                                                                  ptbcontrib

                                                                                  by python-telegram-botPython

                                                                                  ptbtest

                                                                                  by python-telegram-botPython

                                                                                  python-telegram-bot.github.io

                                                                                  by python-telegram-botHTML

                                                                                  website

                                                                                  by python-telegram-botCSS

                                                                                  Compare Chat Libraries with Highest Support

                                                                                  rasa

                                                                                  by RasaHQ

                                                                                  python-telegram-bot

                                                                                  by python-telegram-bot

                                                                                  zulip

                                                                                  by zulip

                                                                                  tinker

                                                                                  by Tencent

                                                                                  taro

                                                                                  by NervJS

                                                                                  Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
                                                                                  Find more libraries
                                                                                  Explore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits​
                                                                                  Save this library and start creating your kit