teleasy.UpdateInfo - noel-friedrich/teleasy GitHub Wiki

Content

Access

Access in Handlers

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:

Direct Access

# Option 1
import teleasy
# then you can use
teleasy.UpdateInfo

# Option 2
from teleasy import UpdateInfo
# then you can use
UpdateInfo

Attributes

Initialization

# import the class
from teleasy import TelegramBot
# init a bot
bot = TelegramBot(<YOUR_TOKEN>)

Data-Attributes

message_id

the unique Identification of the message that led to the Invocation of the Handler

username

the username of the sender
WARNING often times, the sender's Privacy settings result in this not being provided

text

the message text. Incase of a command, e.g. "/say hello", the command will be included in the text

first_name

the first name of the sender. This is nicer to use than .username, because it will always be provided, no matter the privacy settings

data

the date of the message that invoked the creation of the UpdateInfo object (provided by Telegram)

chat_id

unique identifier to the chat the update was caused in

from_bot

if the sender is a bot, will be "true", else "false"

bot

the teleasy.TelegramBot object

user_data

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()

Example

arguments

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

Functions

send_message

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")

respond

same as UpdateInfo.send_message where UpdateInfo.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")

input

get input from user, will cause timeout when user doesn't respond.
similar to inbuilt input() 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?")

input_from

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?")

select

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 the columns=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 🤢"

Example

select_from

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"])

stop_thread

cancel all active processes with the user
for use in cancel handler, see TelegramBot.set_cancel

wait

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 parameter typing=False

UpdateInfo.wait(seconds, typing=True)

# example:
info.wait(5)
⚠️ **GitHub.com Fallback** ⚠️