An warware: telegram bot sami sunan mai amfani

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:

  1. Shigar da ษ—akin karatu ta amfani da pip:
pip install python-telegram-bot
  1. ฦ˜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:

  1. 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
  1. ฦ˜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)
  1. ฦ˜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()
  1. 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.

Shafi posts:

Leave a Comment