A cikin duniyar saฦon take, Telegram ya fito fili a matsayin dandamali mai ฦarfi kuma mai dacewa wanda ke ba masu amfani damar yin hulษa ba kawai tare da wasu mutane ba har ma da bots masu sarrafa kansu. Wadannan bots na iya yin ayyuka da yawa, daga samar da bayanai da nishaษi zuwa sarrafa ayyuka da ayyukan aiki. A matsayin mai haษakawa, ฦirฦirar bot ษin Telegram wanda zai iya samun sunan mai amfani abu ne mai mahimmanci don aiwatarwa, yana sa hulษar ta zama ta sirri da nishadantarwa. A cikin wannan labarin, za mu bincika yadda ake ฦirฦira irin wannan bot ta amfani da yaren shirye-shiryen Python da tattauna ษakunan karatu da ayyukan da ke cikin aikin.
Don ฦirฦirar bot ษin Telegram wanda zai iya samun sunan mai amfani, muna buฦatar yin amfani da Python-telegram-bot ษakin karatu. Wannan ษakin karatu yana ba da API mai dacewa don hulษa tare da Telegram Bot API, yana ba ku damar ฦirฦira, sarrafawa, da sarrafa bots cikin sauฦi.
Kafa ษakin karatu na Python-telegram-bot
Kafin mu shiga cikin maganin, bari mu fara fahimtar yadda ake kafa ษakin karatu na Python-telegram-bot. Don yin wannan, tabbatar da cewa kun shigar da Python akan tsarin ku, sannan ku bi waษannan matakan:
- Shigar da ษakin karatu ta amfani da pip:
pip install python-telegram-bot
- ฦirฦiri sabon bot ta yin magana da BotFaye na Telegram. Zai samar muku da maษallin API na musamman don bot ษin ku.
Tare da shigar da ษakin karatu da maษallin API da aka shirya a hannun ku, mataki na gaba shine ฦirฦirar rubutun Python kuma rubuta lambar don bot na Telegram.
ฦirฦirar bot ษin Telegram don samun sunan mai amfani
Don ฦirฦirar bot ษin ku, kuna buฦatar bin waษannan matakan:
- Shigo da samfuran da ake buฦata daga ษakin karatu na python-telegram-bot:
from telegram import Update from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
- ฦirฦiri aiki don sarrafa umarnin "/fara". Wannan aikin zai gai da mai amfani kuma ya samo sunan farko daga bayanan martaba na Telegram:
def start(update: Update, context: CallbackContext): user_name = update.message.from_user.first_name welcome_msg = f"Hello, {user_name}! Welcome to the bot!" update.message.reply_text(welcome_msg)
- ฦirฦiri babban aiki () don gudanar da bot:
def main(): API_KEY = "YOUR_API_KEY_HERE" updater = Updater(API_KEY) dp = updater.dispatcher dp.add_handler(CommandHandler("start", start)) updater.start_polling() updater.idle()
- Kira babban aikin () a ฦarshen rubutun ku:
if __name__ == '__main__': main()
Tare da wannan lambar, bot ษin ku na Telegram yanzu yana shirye don ษaukar sunan mai amfani da gaishe su.
Fahimtar Code
Bari mu rushe mahimman bangarorin lambar don fahimtar yadda take aiki:
- Da farko, muna shigo da kayayyaki masu mahimmanci daga ษakin karatu na python-telegram-bot. The Sabuntawa aji ne ke da alhakin sarrafa sabuntawa daga Telegram, yayin da CommandHandler da kuma MessageHandler taimaka ayyana yadda ake sarrafa nau'ikan saฦon daban-daban.
- Na gaba, muna ฦirฦirar farko aiki, wanda aka jawo lokacin da mai amfani ya aika umarnin "/ farawa" zuwa bot. Aikin yana fitar da sunan farko na mai amfani ta amfani da shi update.message.daga_user.first_name kuma yana ฦirฦirar saฦon maraba na musamman. A ฦarshe, ana aika saฦon ga mai amfani ta amfani da shi update.message.reply_text().
- A cikin babban () aikin, muna yin ta atomatik Sabuntawa abu tare da maษallin API, ฦara mai sarrafa umarni /farawa, sannan fara madauki na bot.
Ta hanyar bin waษannan matakan da fahimtar mahimman ayyukan da ke tattare da su, zaku iya ฦirฦirar bot ษin Telegram wanda zai ษauki sunan mai amfani kuma yana hulษa da su ta hanyar keษantacce. Ba wai kawai wannan zai ba da ฦarin ฦwarewa ga masu amfani ba, amma kuma zai buษe hanya don aiwatar da ฦarin fasali da ayyuka a cikin bot ษin ku.