|
| 1 | +import re |
| 2 | +from time import sleep |
| 3 | +import requests |
| 4 | + |
| 5 | +from states import States, log |
| 6 | +from constants import * |
| 7 | +from reddit import get_latest_news |
| 8 | + |
| 9 | +__author__ = 'Sathyajith' |
| 10 | + |
| 11 | + |
| 12 | +def get_updates(last_updated): |
| 13 | + log.debug('Checking for requests, last updated passed is: {}'.format(last_updated)) |
| 14 | + sleep(UPDATE_PERIOD) |
| 15 | + response = requests.get(API_BASE + BOT_KEY + '/getUpdates', params={'offset': last_updated+1}) |
| 16 | + json_response = FALSE_RESPONSE |
| 17 | + if response.status_code != 200: |
| 18 | + # wait for a bit, try again |
| 19 | + sleep(UPDATE_PERIOD*20) |
| 20 | + get_updates(last_updated) |
| 21 | + try: |
| 22 | + json_response = response.json() |
| 23 | + except ValueError: |
| 24 | + sleep(UPDATE_PERIOD*20) |
| 25 | + get_updates(last_updated) |
| 26 | + log.info('received response: {}'.format(json_response)) |
| 27 | + return json_response |
| 28 | + |
| 29 | + |
| 30 | +def post_message(chat_id, text): |
| 31 | + log.info('posting {} to {}'.format(text, chat_id)) |
| 32 | + payload = {'chat_id': chat_id, 'text': text} |
| 33 | + requests.post(API_BASE + BOT_KEY + '/sendMessage', data=payload) |
| 34 | + |
| 35 | + |
| 36 | +def handle_incoming_messages(last_updated): |
| 37 | + r = get_updates(last_updated) |
| 38 | + split_chat_text = [] |
| 39 | + if r['ok']: |
| 40 | + for req in r['result']: |
| 41 | + chat_sender_id = req['message']['chat']['id'] |
| 42 | + try: |
| 43 | + chat_text = req['message']['text'] |
| 44 | + split_chat_text = chat_text.split() |
| 45 | + except KeyError: |
| 46 | + chat_text = '' |
| 47 | + split_chat_text.append(chat_text) |
| 48 | + log.debug('Looks like no chat text was detected... moving on') |
| 49 | + try: |
| 50 | + person_id = req['message']['from']['id'] |
| 51 | + except KeyError: |
| 52 | + pass |
| 53 | + |
| 54 | + log.info('Chat text received: {0}'.format(chat_text)) |
| 55 | + r = re.search('(source+)(.*)', chat_text) |
| 56 | + |
| 57 | + if (r is not None and r.group(1) == 'source'): |
| 58 | + if r.group(2): |
| 59 | + sources_dict[person_id] = r.group(2) |
| 60 | + log.debug('Sources set for {0} to {1}'.format(sources_dict[person_id], r.group(2))) |
| 61 | + post_message(person_id, 'Sources set as {0}!'.format(r.group(2))) |
| 62 | + else: |
| 63 | + post_message(person_id, 'We need a comma separated list of subreddits! No subreddit, no news :-(') |
| 64 | + if chat_text == '/stop': |
| 65 | + log.debug('Added {0} to skip list'.format(chat_sender_id)) |
| 66 | + skip_list.append(chat_sender_id) |
| 67 | + post_message(chat_sender_id, "Ok, we won't send you any more messages.") |
| 68 | + |
| 69 | + if chat_text in ('/start', '/help'): |
| 70 | + helptext = ''' |
| 71 | + Hi! This is a News Bot which fetches news from subreddits. Use "/source" to select a subreddit source. |
| 72 | + Example "/source programming,games" fetches news from r/programming, r/games. |
| 73 | + Use "/fetch for the bot to go ahead and fetch the news. At the moment, bot will fetch total of 5 posts from all sub reddits |
| 74 | + I will have this configurable soon. |
| 75 | + ''' |
| 76 | + post_message(chat_sender_id, helptext) |
| 77 | + |
| 78 | + if split_chat_text[0] == '/fetch' and (person_id not in skip_list): |
| 79 | + post_message(person_id, 'Hang on, fetching your news..') |
| 80 | + try: |
| 81 | + sub_reddits = sources_dict[person_id] |
| 82 | + except KeyError: |
| 83 | + post_message(person_id, ERR_NO_SOURCE) |
| 84 | + else: |
| 85 | + summarized_news = get_latest_news(sources_dict[person_id]) |
| 86 | + post_message(person_id, summarized_news) |
| 87 | + last_updated = req['update_id'] |
| 88 | + with open('last_updated.txt', 'w') as f: |
| 89 | + f.write(str(last_updated)) |
| 90 | + States.last_updated = last_updated |
| 91 | + log.debug( |
| 92 | + 'Updated last_updated to {0}'.format(last_updated)) |
| 93 | + f.close() |
0 commit comments