teleasy.TelegramBot - noel-friedrich/teleasy GitHub Wiki

Content

Access

# Option 1
import teleasy
# then you can use
teleasy.TelegramBot(<YOUR_TOKEN>)

# Option 2
from teleasy import TelegramBot
# then you can use
TelegramBot(<YOUR_TOKEN>)

Attributes

Initialization

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

Functions

__init__

TelegramBot.__init__(self, token: str)
# initializes bot object, takes token as string

# EXAMPLE USE
bot = TelegramBot(<YOUR_TOKEN>)

enable_console_logging

TelegramBot.enable_console_logging(self, state=True: bool)
# changes TelegramBot.console_logging to provided State
# once True, the bot will output information about what it's
# doing to the console using print()

# EXAMPLE USE
bot.enable_console_logging()

set_error

TelegramBot.set_error(self, new_handler: function)
# set a new error handler to be called when an Update
# HandlerThread raises an Exception
# the standard function is:
def error_handler(info: UpdateInfo):
    info.respond(f"An Error occured")
# the passed UpdateInfo object will contain a special
# info.error attribute, which is a dictionary containing
# information about the error. It's return value will be ignored

# EXAMPLE USE
def my_error_handler(info: UpdateInfo):
    for key, value in info.error.items():
        info.respond(f"{key}: {value}")
bot.set_error(my_error_handler)

set_standart_timeout

TelegramBot.set_standart_timeout(self, new_timeout: int):
# sets the standard timeout time (in seconds), after which the bot
# will time out when awaiting an input. This can
# be overwritten in the info.input or info.select function
# using the timeout=30 parameter, if not defined, the standard
# timeout time will be used

# EXAMPLE USE
bot.set_standard_timeout(60)

set_process_running_msg

TelegramBot.set_process_running_msg(self, new_msg: str)
# sets the message that will be sent to the user when he tries
# to invoke a new bot command when he has a waiting input
# (which he can cancel using the defined cancel-command (normally /cancel))
# the standart message is: "failed: you already have an active process"

# EXAMPLE USE
bot.set_process_running_msg("please first answer your active input process")

set_timeout_msg

TelegramBot.set_timeout_msg(self, new_msg: str)
# sets the message to be sent to the user when he doesn't answer
# an input process (e.g. invoked via info.input or info.select)
# in the required timeout time (can be changed via TelegramBot.set_standart_timeout)
# the standart message is: "timed out. took too long to respond"

# EXAMPLE USE
bot.set_timeout_msg("you haven't answered in the required time. please exit")

set_cancel_command

TelegramBot.set_cancel_command(self, new_command: str)
# changes the cancel command to the new_command string specified
# the cancel command can be used by the user to cancel an
# active input process.
# the standard cancel command is: "cancel"

# EXAMPLE USE
bot.set_cancel_command("let_it_go")

set_cancel

TelegramBot.set_cancel(self, new_command: str, new_func: function)
# changes the cancel command to the new_command string specified
# (see TelegramBot.set_cancel_command for more information)
# changes the cancel function to the provided new_func
# the standard cancel function is:
def cancel_func(info: UpdateInfo):
    info.stop_thread()
    return "Cancelled Active Process"
# it will be given the UpdateInfo similarly to a normal message handler
# for the cancel function to work, it must contain info.stop_thread(),
# which cancels any active processes
# it's return value will be texted to the user

# EXAMPLE USE
def new_cancel_func(info: UpdateInfo):
    info.stop_thread()
    return "Success! You cancelled your active Process, dear User"
bot.set_cancel("let_it_go", new_cancel_func)

set_command

TelegramBot.set_command(self, command: str, command_handler: function)
# adds a command to the TelegramBot.commands dictionary
# the command_handler will be given an UpdateInfo object and will be
# invoked when a user calls this command in the chat
# (the command: str may start with the "/" or not, it will automagically correct itself)

# EXAMPLE USE
def help_command_handler(info: UpdateInfo):
    return "You requested Help? I can give you Help!"
bot.set_command("help", help_command_handler)

set_normal

TelegramBot.set_normal(self, normal_handler: function)
# changes the command handler to respond to normal messages
#     (a normal message is a message which is not recognized
#      to be a command nor an input to an open input request)
# the handler will be given an UpdateInfo object as a parameter

# EXAMPLE USE
def normal_message_handler(info: UpdateInfo):
    return "I don't understand what you mean??"
bot.set_normal(normal_message_handler)

send_message

TelegramBot.send_message(self, chat_id: str, msg: str, parse_mode="") -> bool
# sends a message in the specified chat_id
# for parse_mode s, see https://core.telegram.org/bots/api#formatting-options

# EXAMPLE USE
bot.send_message(<YOUR_CHAT_ID>, "helloo there, general kenobi!")

start

TelegramBot.start(self, interval=0.3, console_logging=None)
# start the TelegramBot.loop with the specified interval:
# each {interval} seconds, a request will be made to the telegram API.
# specify console_logging=True to log Bot Activity into the Console
#     (this is great for debugging)

# this is one of two starting options. the pendant is:
TelegramBot.start_with_interface

# EXAMPLE USE
bot.start()

start_with_interface

TelegramBot.start_with_interface(self, interval=0.3, end=True)
# start the TelegramBot.loop with the specified interval:
# each {interval} seconds, a request will be made to the telegram API.
# additionally, a tkinter window containing a console and an
# activity graph will open, in which you can monitor the bot activity
# if end=True, then the bot will stop running when you close the interface,
# if end=False, then the bot will continue running when you close the interface
# (you can see an example screenshot of the 1.0.1 Interface below)

# this is one of two starting options. the pendant is:
TelegramBot.start

# EXAMPLE USE
bot.start_with_interface()

Telegram Bot Monitor

Other Attributes

token

TelegramBot.token: str
# stores the token specified in TelegramBot.__init__

commands

TelegramBot.commands: dict
# stores the command handlers in connection with their command-strings
# is prefilled with the cancel command string and handler

# a typical bot.commands dict might look like:
bot.commands = {
    "cancel": cancel_command_handler,
    "help": help_command_handler,
    "start": start_command_handler
}

# the typestructure:
dict( str : function )

log_str

TelegramBot.log_str: str
# contains the log string, will only be filled if
# TelegramBot._logging == True, which will be set to False
# when using TelegramBot.start and set to True
# when using TelegramBot.start_with_interface (which displays the log string)
⚠️ **GitHub.com Fallback** ⚠️