Rohin Ramesh
Published © GPL3+

telegramchatbot

It is a nlp project a chatbot that replies as per the message given

IntermediateProtip5 hours228
telegramchatbot

Things used in this project

Software apps and online services

Jupyter Notebook
Jupyter Notebook

Story

Read more

Schematics

the process of NLP

Code

NLP_bot-main.zip

Python
run the code in ide
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
updater = Updater(token='TOKEN', use_context=True) #Replace TOKEN with your token string
dispatcher = updater.dispatcher
import telegram
bot = telegram.bot(token='TOKEN') #Replace TOKEN with your token string
def hello(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id, text='Hello, World')
    hello_handler = CommandHandler('hello', hello)
dispatcher.add_handler(hello_handler)
updater.start_polling()
from bs4 import BeautifulSoup
import requests
from random import randint

class JokeGenerator:
    
    def __init__(self):
        self.cache = []
        self.urls = ['https://nekdo.ru/random/',
                     'http://anekdotme.ru/random']
        self.find_args = [('div', 'text'),
                          ('div', 'anekdot_text')]
    
    def add_url(self, url, args):
        self.urls.append(url)
        self.find_args.append(args)
    
    def upd_cache(self):
        ind = randint(0, len(self.urls) - 1)
        url = self.urls[ind]
        args = self.find_args[ind]

        page = requests.get(url).text
        soup = BeautifulSoup(page, 'html.parser')
        
        for t in soup.find_all(args[0], args[1]):
            self.cache.append(t.text.strip().replace('\n', ' '))
    
    def generate_joke(self):
        if not self.cache:
            self.upd_cache()
            
        joke = self.cache[0]
        del self.cache[0]

        return joke
    
    def clear_cache(self):
        self.cache = []
© 2021 GitHub, Inc.

Credits

Rohin Ramesh
5 projects • 4 followers
Contact

Comments

Please log in or sign up to comment.