teleasy.UpdateInfo - noel-friedrich/teleasy GitHub Wiki
When defining Handler Methods, an
UpdateInfo
object will be passed as it's parameter.
# an example handler message for the "/help" command
def help_command_handler(info: UpdateInfo):
return "This is all the Help i can provide"
# tell your bot to invoke the function on the "help" command
bot.set_command("help", help_command_handler)
An UpdateInfo
object will be passed to:
- the normal message handler
- a command handler (like the example above)
- the cancel command handler
# Option 1
import teleasy
# then you can use
teleasy.UpdateInfo
# Option 2
from teleasy import UpdateInfo
# then you can use
UpdateInfo
# import the class
from teleasy import TelegramBot
# init a bot
bot = TelegramBot(<YOUR_TOKEN>)
the unique Identification of the message that led to the Invocation of the Handler
the username of the sender
WARNING often times, the sender's Privacy settings result in this not being provided
the message text. Incase of a command, e.g. "/say hello", the command will be included in the text
the first name of the sender. This is nicer to use than .username, because it will always be provided, no matter the privacy settings
the date of the message that invoked the creation of the
UpdateInfo
object (provided by Telegram)
unique identifier to the chat the update was caused in
if the sender is a bot, will be "true", else "false"
the teleasy.TelegramBot object
the
teleasy.UserData
object associated with the current chat
will always be the same object, can be used to store info about the user:
from teleasy import TelegramBot, UpdateInfo
bot = TelegramBot(<MY_API_TOKEN>)
def fetch_data(info: UpdateInfo):
return info.user_data["favorite_color"]
# you can also use user_data.get
return info.user_data.get('favorite_color')
def set_data(info: UpdateInfo):
favorite_color = info.input("What is your favorite Color?")
info.user_data["favorite_color"] = favorite_color
# you can also use user_data.set
info.set("favorite_color", favorite_color)
return f"set favorite_color: {favorite_color}"
bot.set_command("fetch", fetch_data)
bot.set_command("set", set_data)
bot.set_normal(lambda info: "Use /set or /fetch to set and fetch your favorite color!")
bot.start()
empty if the message isn't a command
will contain all arguments
if info.text == "/say hello this is ":
info.arguments = ["hello", "this", "is"]
if info.text == '/say hello "this is"':
info.arguments = ["hello", "this is"]
if info.text == "/say hello 'this is'":
info.arguments = ["hello", "this is"]
if info.text == "/say":
info.arguments = []
if info.text == "hello this is":
info.arguments = []
# because it isn't a command
same as TelegramBot.send_message or
UpdateInfo.bot.send_message
UpdateInfo.send_message(chat_id: str, message: str, parse_mode="")
# example:
info.send_message(<MY_CHAT_ID>, "hello there, <i>general_kenobi</i>", parse_mode="html")
same as
UpdateInfo.send_message
whereUpdateInfo.chat_id
is automagically filled
similar to returning a string, will respond message to user
UpdateInfo.respond(message: str, parse_mode="")
# example:
info.respond("hello there, general kenobi")
get input from user, will cause timeout when user doesn't respond.
similar to inbuiltinput()
method in python
default timeout can be changed with TelegramBot.set_standart_timeout function
UpdateInfo.input(message: str, timeout=default_timeout)
# example:
age = info.input("how old are you?")
similar as
UpdateInfo.input
, only that you can specify a chat_id
UpdateInfo.input_from(chat_id: str, message: str, timeout=default_timeout)
# example:
age = info.input_from(info.chat_id, "how old are you?")
# is the same as
age = inf.input("how old are you?")
display a button panel to the user to choose one, returns the answer
specifiy number of columns next to each other in the button layout using thecolumns=1
parameter
UpdateInfo.select(message: str, options: list, columns=1)
# example:
color = info.select("choose your favorite color:", ["Blue 🥶", "Green 🤢", "Red 👹", "Yellow 😃"], columns=2)
# when user clicks on "Green 🤢", color == "Green 🤢"
similar to
UpdateInfo.select
, only that you can specify a chat_id
UpdateInfo.select_from(chat_id: str, message: str, options: list, columns=1)
# example:
color = info.select_from(info.chat_id, "choose a color", ["r", "g", "b"])
# is the same as
color = info.select("choose a color", ["r", "g", "b"])
cancel all active processes with the user
for use in cancel handler, see TelegramBot.set_cancel
similar to
time.sleep
, but the bot will display the "typing..." animation in telegram app
typing functionality doesn't seem to work well in telegram web
can be deactivated using parametertyping=False
UpdateInfo.wait(seconds, typing=True)
# example:
info.wait(5)