forked from cganterh/le
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6838a2f
Showing
6 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/env/ | ||
__pycache__/ | ||
/config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
python-telegram-bot | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#! /usr/bin/env python3 | ||
# -*- coding: UTF-8 -*- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('---------------------------') |