Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cganterh committed Jan 8, 2017
0 parents commit 6838a2f
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/env/
__pycache__/
/config.json
17 changes: 17 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
env_dir = env
activate = . $(env_dir)/bin/activate
python = python3
req_file = requirements.txt
module = tcad_bot

.PHONY: run
run: | $(env_dir)
$(activate) && python -m $(module)

$(env_dir):
$(python) -m venv $@
$(activate) && pip install -r $(req_file)

.PHONY: clean
clean:
rm -rf $(env_dir)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
python-telegram-bot
requests
2 changes: 2 additions & 0 deletions tcad_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /usr/bin/env python3
# -*- coding: UTF-8 -*-
35 changes: 35 additions & 0 deletions tcad_bot/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#! /usr/bin/env python3
# -*- coding: UTF-8 -*-

import json
import logging

from telegram.ext import CommandHandler, Filters, MessageHandler, Updater

from .handlers import hello, get_uv_index, parse_normal_message


with open('config.json') as f:
config = json.load(f)

updater = Updater(token=config['telegram_token'])

logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)

updater.dispatcher.add_handler(
CommandHandler('hi', hello)
)

updater.dispatcher.add_handler(
CommandHandler('uvindex', get_uv_index)
)

updater.dispatcher.add_handler(
MessageHandler(Filters.all, parse_normal_message)
)

updater.start_polling()
updater.idle()
69 changes: 69 additions & 0 deletions tcad_bot/handlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from difflib import get_close_matches
from logging import getLogger
from pprint import pprint
from random import choice, random

import requests


rooms = {}
logger = getLogger()


def hello(bot, update):
bot.sendMessage(
chat_id=update.message.chat_id,
text="Hi all, I'm the TCAD bot. More features comming soon."
)


def get_uv_index(bot, update):
response = requests.get(
'http://archivos.meteochile.gob.cl/portaldmc/meteochile/js/'
'indice_radiacion.json'
)

data = response.json()
radiation_data = data['RadiacionUVB']

radiation_stgo = next(
filter(lambda p: p['nombre'] == 'SANTIAGO', radiation_data)
)

bot.sendMessage(
chat_id=update.message.chat_id, text=radiation_stgo['indiceobs'])


def parse_normal_message(bot, update):
cur_message = update.message.text
chat_id = update.message.chat_id
user = update.message.from_user.id

if chat_id not in rooms:
rooms[chat_id] = {
'dict': {},
'last_message': '',
'last_user': None,
}

d = rooms[chat_id]['dict']
last_message = rooms[chat_id]['last_message']
last_user = rooms[chat_id]['last_user']

if cur_message and last_message and last_user != user:
d[last_message] = cur_message

rooms[chat_id]['last_message'] = cur_message if cur_message else ''
rooms[chat_id]['last_user'] = user

previous_messages = d.keys()
similar_messages = get_close_matches(cur_message, previous_messages)

if len(similar_messages) > 0 and random() > 0.5:
choosen_message = choice(similar_messages)
response = d[choosen_message]
bot.sendMessage(chat_id=update.message.chat_id, text=response)
rooms[chat_id]['last_message'] = response

pprint(rooms)
print('---------------------------')

0 comments on commit 6838a2f

Please sign in to comment.