Python’da TelegramBoT yaratish #1
Hammaga Salom! Bugun telegram bot yaratamiz. TelegramBot ancha qiziq 🙂 Hullas bizga telegram bot apilari kerak bo’ladi. Buning uchun buyruqlar qatori(cmd)ga kiramiz. Va quydagi buyruqni bajaramiz:
pip install pyTelegramBotAPI
Mana telegrambotni apilarini o’rnatib oldik. Ana endi kod yozishga o’tamiz. myfisrtbot.py db yangi fayl ochamiz.
Endi telegramdan @botfather deb izlaymiz va u yerdan bot uchun token olamiz.
/newbot
botname = myfirstbotpython
botusername = myfirstbotpython_bot
Shulardan keyin @botfather sizga token beradi:
Mana token ham oldik. Endi myfirstbot.py faylga kiramiza va quyidagi kodni yozamiz:
import telebot bot = telebot.TeleBot('645603723:AAFF21RDZIW9mqEXOAM6-2lNTrdh6vzflgI') @bot.message_handler(commands=['start']) def start(message): name = str(message.from_user.first_name) bot.send_message(message.from_user.id ,'Salom ' + name) #bu yerda user /start bosganda Isminmi olib Salom qowb yuboradi #message bu json telegram server bizga xabarda json api yuboradi uni ichida userga tegishli #barcha narsa bor @bot.message_handler(content_types=['text']) def send(message): text = message.text if text == 'Python': bot.send_message(message.from_user.id , 'Salom Python Programist') elif text == 'Java': bot.send_message(message.from_user.id , 'Salom Java Programist') elif text == 'Php': bot.send_message(message.from_user.id , 'Salom Php Programist') elif text == 'C#': bot.send_message(message.from_user.id , 'Salom C# Programist') else: bot.send_message(message.from_user.id , 'Uzur men sizi tanimayaman 😑😂') bot.polling(none_stop= True) # bu botimiz ochib qolmasligi uchun!
Mana soddagina bot yasab oldik.
Endi buni ishga tushirib, tekshirib ko’ramiz.
Mana soddagina bot yasab oldik. Keyingi darslarmizda tugma qo’yishni, sqlite3 ma’lumotlar omborini ulashni o’rganamiz 😊
Hammaga xayr!
Savolaringiz bo’lsa, izohda qoldirishingiz mumkin.
Web Dasturlash
Python’da TelegramBoT yaratish #1