Python’da TelegramBoT yaratish #1

Python’da TelegramBoT yaratish #1

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.

pythonda telegrambot yaratish 1 65e6111fb1ee5

/newbot

botname = myfirstbotpython

botusername = myfirstbotpython_bot

Shulardan keyin @botfather sizga token beradi:

pythonda telegrambot yaratish 1 65e61120cf43a

Mana token ham oldik. Endi myfirstbot.py faylga kiramiza va quyidagi kodni yozamiz:

pythonda telegrambot yaratish 1 65e61121e4f4c

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.

pythonda telegrambot yaratish 1 65e61122d2d95

Endi buni ishga tushirib, tekshirib ko’ramiz.

pythonda telegrambot yaratish 1 65e61123bee48

pythonda telegrambot yaratish 1 65e61124b913b

pythonda telegrambot yaratish 1 65e61125da229

pythonda telegrambot yaratish 1 65e61126e8ce4

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