Python and telegram bot. Create.
For the start I need to simple telegram bot. First function - take audio, and secong - give audio.
I make this in ubuntu.
Test bot
pip install python-telegram-bot
/botFather in telegram and create /newbot
# mastrobot_example.py
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
# function to handle the /start command
def start(update, context):
update.message.reply_text('start command received')
# function to handle the /help command
def help(update, context):
update.message.reply_text('help command received')
# function to handle errors occured in the dispatcher
def error(update, context):
update.message.reply_text('an error occured')
# function to handle normal text
def text(update, context):
text_received = update.message.text
update.message.reply_text(f'did you said "{text_received}" ?')
def main():
TOKEN = "insert here your token and don't share it with anyone!"
# create the updater, that will automatically create also a dispatcher and a queue to
# make them dialoge
updater = Updater(TOKEN, use_context=True)
dispatcher = updater.dispatcher
# add handlers for start and help commands
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help))
# add an handler for normal text (not commands)
dispatcher.add_handler(MessageHandler(Filters.text, text))
# add an handler for errors
dispatcher.add_error_handler(error)
# start your shiny new bot
updater.start_polling()
# run the bot until Ctrl-C
updater.idle()
if __name__ == '__main__':
main()
$ nano test.py
and insert code
$ python test.py
Hooray, the bot works!
Simple echo bot
The main function is to receive and send audio messages to the bot. Therefore, I will begin to deal with the code that does this.
To begin with, I will make a simple echo - bota.
I create a config.py file and add token = "xxxxx" there
the code
import config
import telebot
bot = telebot.TeleBot(config.token)
@bot.message_handler(content_types=["text"])
def repeat_all_messages(message): #The name of the function doesn't matter.
bot.send_message(message.chat.id, message.text)
if __name__ == '__main__':
bot.infinity_polling()
I launch - it works!
Buttons
import telebot
from telebot import types
bot = telebot.TeleBot("Put Your Token Here")
@bot.message_handler(content_types=["text"])
def any_msg(message):
# Create a keyboard and each of the buttons (2 in a row)
keyboard = types.InlineKeyboardMarkup(row_width=2)
url_button = types.InlineKeyboardButton(text="URL", url="https://ya.ru")
callback_button = types.InlineKeyboardButton(text="Callback", callback_data="test")
switch_button = types.InlineKeyboardButton(text="Switch", switch_inline_query="Telegram")
keyboard.add(url_button, callback_button, switch_button)
bot.send_message(message.chat.id, "I am a message from normal mode", reply_markup=keyboard)
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
# If the message is from a chat with the bot
if call.message:
if call.data == "test":
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Puff")
# Notification at the top of the screen
bot.answer_callback_query(callback_query_id=call.id, show_alert=False, text="Push!")
# If the message is from inline mode
elif call.inline_message_id:
if call.data == "test":
bot.edit_message_text(inline_message_id=call.inline_message_id, text="Bdish")
# The simplest inline handler for non-null requests
@bot.inline_handler(lambda query: len(query.query) > 0)
def query_text(query):
kb = types.InlineKeyboardMarkup()
kb.add(types.InlineKeyboardButton(text="Click me", callback_data="test"))
results = []
# Please note: instead of text - an input_message_content object with text!
single_msg = types.InlineQueryResultArticle(
id="1", title="Press me",input_message_content=types.InputTextMessageContent(message_text="I am an inline message"),
reply_markup=kb
)
results.append(single_msg)
bot.answer_inline_query(query.id, results)
if __name__ == '__main__':
bot.infinity_polling()
Continuation coming soon.
Thank you for your attention.
Have a good day!