Image

(अंतिम परिवर्तन: 04.05.2024)

पायथन चैटबॉट के प्रदर्शन का परीक्षण करने के लिए, हमने तैयार प्रश्नों और उत्तरों का उपयोग किया ready pre -कॉन्फ़िगर किया गया टेलीग्राम बॉट सर्वर। यह बॉट पहले से तैयार प्रश्नों का उत्तर पहले से तैयार उत्तरों के साथ दे सकता है, जोanswers.txt फ़ाइल में निहित हैं, और एक लॉग भी रखता है।

आइए इंस्टालेशन शुरू करें:

source my-tel-bot/bin/activate
pip install fuzzywuzzy
pip install python-Levenshtein

mkdir /root/my-tel-bot/data
chmod 775 /root/my-tel-bot/data

आइए प्रश्नों और उत्तरों के साथ एक फ़ाइल तैयार करें:

nano /root/my-tel-bot/data/answers.txt

u: नमस्ते
चैटबॉट आपका स्वागत करता है
u: नाम क्या है?
पापा!
u: आपकी उम्र कितनी है
अपेक्षाकृत कम
u: तुम क्या कर सकते हो
आप मेरे डेटाबेस मेंanswers.txt फ़ाइल में जोड़ सकते हैं

आइए अपने डेमो बॉट को मौजूदा डेमो बॉट से बदलें:

/root/my-tel-bot/bot.py

import telebot
import os
from fuzzywuzzy import fuzz

# Create a bot, write your own token
bot = telebot.TeleBot('Key received from @BotFather')

# Loading a list of phrases and answers into an array
mas=[]
if os.path.exists('data/answers.txt'):
f=open('data/answers.txt', 'r', encoding='UTF-8')
for x in f:
if(len(x.strip()) > 2):
mas.append(x.strip().lower())
f.close()

# Using fuzzywuzzy, we calculate the most similar phrase and give the next element of the list as an answer
def answer(text):
try:
text=text.lower().strip()
if os.path.exists('data/answers.txt'):
a = 0
n = 0
nn = 0
for q in mas:
if('u: ' in q):

# Using fuzzywuzzy we get how similar two strings are
aa=(fuzz.token_sort_ratio(q.replace('u: ',''), text))
if(aa > a and aa!= a):
a = aa
nn = n
n = n + 1
s = mas[nn + 1]
return s
else:
return 'Error'
except:
return 'Error'

# /Start Command
@bot.message_handler(commands=["start"])
def start(m, res=False):
bot.send_message(m.chat.id, 'I am in touch. Write me hello )')

# Receiving messages from a user
@bot.message_handler(content_types=["text"])
def handle_text(message):

# Logging
f=open('data/' + str(message.chat.id) + '_log.txt', 'a', encoding='UTF-8')
s=answer(message.text)
f.write('u: ' + message.text + '\n' + s +'\n')
f.close()

# Sending a response
bot.send_message(message.chat.id, s)

# Launching the bot
bot.polling(none_stop=True, interval=0)

हो गया, स्क्रिप्ट में अपनी टोकन कुंजी निर्दिष्ट करना और सेवा पुनः आरंभ करना न भूलें:

service my-tel-bot restart



No Comments Yet