Skip to content

Commit

Permalink
Merge pull request #3 from aayushmau5/telegram_bot
Browse files Browse the repository at this point in the history
Added Handler for unknown Commands
  • Loading branch information
Sabreen-Parveen authored Oct 5, 2020
2 parents 3dae401 + 69bab69 commit 853168a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data_file.json
__pycache__
34 changes: 33 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import random
import requests
import re
from bs4 import BeautifulSoup
from html import escape

Expand All @@ -9,6 +10,9 @@

from config import bot_name, token

isSleeping = True
anyCommand = re.compile(r'\*')

root = logging.getLogger()
root.setLevel(logging.INFO)

Expand Down Expand Up @@ -41,6 +45,8 @@ def welcome(update, context, new_member):


def xkcd(update, context):
if(isSleeping):
return
message = update.message
chat_id = message.chat.id
msg_text = message.text
Expand Down Expand Up @@ -82,7 +88,8 @@ def xkcd(update, context):


def start(update, context):

global isSleeping
isSleeping = False
chat_id = update.message.chat.id

text = (
Expand All @@ -92,8 +99,21 @@ def start(update, context):
)
context.bot.send_message(chat_id=chat_id, text=text)

def stop(update,context):
global isSleeping
isSleeping = True
chat_id = update.message.chat.id

text = (
"Bot went to sleep\n"
"To wake the bot, use /start command\n"
)
context.bot.send_message(chat_id=chat_id, text=text)


def help(update, context):
if(isSleeping):
return
help_text = (
"I understand these commands: \n"
"/help - List the commands that I understand \n"
Expand All @@ -109,6 +129,16 @@ def help(update, context):
disable_web_page_preview=True,
)

def unknown(update,context):
if(isSleeping):
return
chat_id = update.message.chat.id

text = (
"Sorry I don't know that command\n"
)
context.bot.send_message(chat_id=chat_id, text=text)


def lock(update, context):
""" Locks the chat, so only the invitee can change settings """
Expand All @@ -134,6 +164,8 @@ def main():
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("xkcd", xkcd))
dp.add_handler(CommandHandler("stop", stop))
dp.add_handler(MessageHandler(Filters.regex(r"/*"),unknown))
dp.add_handler(MessageHandler(Filters.status_update, check))

"""updater.start_webhook(listen="0.0.0.0",
Expand Down
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
bot_name = "USERNAME"
token = "TOKEN"
bot_name = "aayrodev_bot"
token = "795615149:AAH1OVK_27ePxZmEjguqdX-3pJs5OA-oWuE"
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
requests==2.24.0
python_telegram_bot==12.8
beautifulsoup4==4.9.2
telegram==0.0.1
beautifulsoup4==4.9.2

0 comments on commit 853168a

Please sign in to comment.