Telegram Bot.       .
 

 


         Telegram     Python.        ,      ,     ,      .    ,     ,      ,       ,          .      ,        Telegram        Python.    Telegram        ,      .





 

Telegram Bot.       .





 1: 




 ,      ,       ,           .     ,   Telegram       .

       ....    ,        .   -     .      ....  4 .

  Telegram    ,          .     ,  , ,    .

   Telegram     , ,       .       , ,      .

         Telegram   Python    python-telegram-bot.        ,      ,       .

      ,    ,    .      ,      , API   .

       Python,              Telegram.    ,                    Telegram.

              Telegram.




 2:  




       Telegram,        .

 ,      Telegram,      .       BotFather,   Telegram      .

BotFather    ,       .        BotFather  "/newbot"  .

            username,     "bot". , "MyAwesomeBot"  "CoolBot123_bot".

 ,      username   , BotFather       ,       API Telegram    .       ,         .

        Telegram!     ,    ,   ,    BotFather.




 3:  




,         ,        Python.       python-telegram-bot.

Python-telegram-bot        API Telegram,         Telegram   Python.     ,            .

    python-telegram-bot   .     pip     Python.



    :



pip install python-telegram-bot



   python-telegram-bot,       .

   Python    python-telegram-bot:



import telegram

from telegram.ext import Updater, CommandHandler



      Updater,      Telegram       .       ,     BotFather:



updater = Updater(token='YOUR_TOKEN')



 "YOUR_TOKEN"    .



       "/start",         :



def start(update, context):

....context.bot.send_message(chat_id=update.effective_chat.id, text="Hello, I'm a bot!")



     "Hello, I'm a bot!"    "/start".

       Updater,     :



updater.dispatcher.add_handler(CommandHandler('start', start))



     "/start" Updater.



updater.start_polling()



      Telegram     .

,    ,      "/start" ,     .



         Python    python-telegram-bot.   ,   Updater,    "/start"   .              .

     ,    . ,       "/help",      :



def help(update, context):

....text = "Available commands:\n/start  start the bot\n/help  show available commands"

....context.bot.send_message(chat_id=update.effective_chat.id, text=text)



updater.dispatcher.add_handler(CommandHandler('help', help))



     "/help"       .

        . ,       "Hi",       "Hello!":



def message_handler(update, context):

....text = update.message.text.lower()

....if text == 'hi':

........context.bot.send_message(chat_id=update.effective_chat.id, text="Hello!")

....

updater.dispatcher.add_handler(MessageHandler(Filters.text, message_handler))



     ,    .     "hi",     "Hello!".



           ,     .



         Python    python-telegram-bot.      "/start"  "/help",      .           ,            .




 4:   




 ,        ,     ,        .          .



 ,     ,      .      API  They Said So,         .



 ,    API-   They Said So.      requests          .



import requests



def get_quote():

....url = "https://api.theysaidso.com/quote/random"

....headers = {"Accept": "application/json"}

....response = requests.get(url, headers=headers)

....quote = response.json()["contents"]["quote"]

....returnquote



   GET-    They Said So       JSON.         .

,       ,       "/quote",     get_quote     :



def quote(update, context):

....quote = get_quote()

....context.bot.send_message(chat_id=update.effective_chat.id, text=quote)



updater.dispatcher.add_handler(CommandHandler('quote', quote))



    "/quote",    get_quote     .

    ,       .      API  TheCatAPI,        .

def get_cat_image_url():

....url = "https://api.thecatapi.com/v1/images/search"

....response = requests.get(url)

....image_url = response.json()[0]["url"]

....return image_url



   GET-    TheCatAPI        JSON.    URL    .

      "/cat",     get_cat_image_url      :



def cat(update, context):

....image_url = get_cat_image_url()

....context.bot.send_photo(chat_id=update.effective_chat.id, photo=image_url)



updater.dispatcher.add_handler(CommandHandler('cat', cat))



        ,        .      API  OpenWeatherMap.

 ,    API-   OpenWeatherMap    pyowm,     API.     ,        :



import pyowm



owm = pyowm.OWM('your-api-key')



def get_weather(city):

....observation = owm.weather_at_place(city)

....weather = observation.get_weather()

....temperature = weather.get_temperature('celsius')['temp']

....status = weather.get_detailed_status()

....return f"Current weather in {city}: {status}. Temperature: {temperature}C"



       ,  API  OpenWeatherMap.       ,        .

      "/weather",     get_weather         :



def weather(update, context):

....text = update.message.text

....city = text.split(' ')[1]

....weather = get_weather(city)

....context.bot.send_message(chat_id=update.effective_chat.id, text=weather)



updater.dispatcher.add_handler(CommandHandler('weather', weather))



    "/weather",           get_weather     .      .

     :   ,            .      ,           .




  .


   .

   ,     (https://www.litres.ru/book/artem-demidenko/telegram-bot-rukovodstvo-po-sozdaniu-bota-v-messendzhere-t-69163303/)  .

      Visa, MasterCard, Maestro,    ,   ,     ,  PayPal, WebMoney, ., QIWI ,       .


