-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.py
56 lines (37 loc) · 1.47 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import logging
from telegram.ext import *
import responses
API_KEY = 'YOUR API KEY'
# Set up the logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logging.info('Starting Bot...')
def start_command(update, context):
update.message.reply_text('Hello there! I\'m a bot. What\'s up?')
def help_command(update, context):
update.message.reply_text('Try typing anything and I will do my best to respond!')
def custom_command(update, context):
update.message.reply_text('This is a custom command, you can add whatever text you want here.')
def handle_message(update, context):
text = str(update.message.text).lower()
logging.info(f'User ({update.message.chat.id}) says: {text}')
# Bot response
response = responses.get_response(text)
update.message.reply_text(response)
def error(update, context):
# Logs errors
logging.error(f'Update {update} caused error {context.error}')
# Run the programme
if __name__ == '__main__':
updater = Updater(API_KEY, use_context=True)
dp = updater.dispatcher
# Commands
dp.add_handler(CommandHandler('start', start_command))
dp.add_handler(CommandHandler('help', help_command))
dp.add_handler(CommandHandler('custom', custom_command))
# Messages
dp.add_handler(MessageHandler(Filters.text, handle_message))
# Log all errors
dp.add_error_handler(error)
# Run the bot
updater.start_polling(1.0)
updater.idle()