def broadcast_message(message): t = bot_util.read_one_string_file(novgorod_weather_bot.TOKEN_FILENAME) botan_t = bot_util.read_one_string_file(novgorod_weather_bot.BOTAN_TOKEN_FILENAME) bot = novgorod_weather_bot.NovgorodWeatherBot(t, name="NovgorodWeatherBot", botan_token=botan_t) lines = open(bot.chats_file, 'r').readlines() for l in lines: l = int(l) bot.send_response(l, message)
def broadcast_message(message): t = bot_util.read_one_string_file(novgorod_weather_bot.TOKEN_FILENAME) botan_t = bot_util.read_one_string_file(novgorod_weather_bot.BOTAN_TOKEN_FILENAME) bot = novgorod_weather_bot.NovgorodWeatherBot(t, name="NovgorodWeatherBot") lines = open(bot.chats_file, 'r').readlines() for l in lines: l = int(l) bot.send_response(l, message)
def broadcast_message(message): t = bot_util.read_one_string_file(fivethreecyclingbot.TOKEN_FILENAME) bot = fivethreecyclingbot.FiveThreeCyclingBot(t, name="FiveThreeCyclingBot") lines = open(bot._chats_file, 'r').readlines() for l in lines: # l = int(l) bot.send_response(l, message, HTML=True)
def _read_previous_update_date(self): u = bot_util.read_one_string_file(self._PREVIOUS_UPDATE_DATE_FILENAME) if u == "" or None == u: return 0 return int(u)
def _read_previous_update_date(self): u = bot_util.read_one_string_file(self._PREVIOUS_UPDATE_DATE_FILENAME) if u == '' or u is None: return 0 return int(u)
with open(ADMIN_FILENAME) as admins_file: lines = admins_file.readlines() if lines: admins = list() for line in lines: admins.append(line[:-1]) admins = set(admins) class FiveThreeCyclingBot(TelegramBot): def __init__(self, token, name): TelegramBot.__init__(self, token, name) def _process_message(self, user_id, chat_id, text): if text == '/start' or text == '/start@FiveThreeCyclingBot': response = """ Присылаю новые посты по хештегу #53cycling """ else: response = "" if response: success = self.send_response(chat_id, response=response) return success return False if __name__ == "__main__": t = bot_util.read_one_string_file(TOKEN_FILENAME) bot = FiveThreeCyclingBot(t, name="fivethreecyclingbot") bot.start_poll()
def build_message(_hashtag): url = get_newsfeed_search_hashtag_url(_hashtag) response_text = bot_util.urlopen(url) if not response_text: print "Failed to get data from VK" return None response_json = json.loads(response_text) if 'response' not in response_json: print "No 'response' in response" print response_json return None response = response_json['response'] if 'items' not in response: print "No 'items' in 'response'" return None items = response['items'] if len(items) == 0: print "Length of 'items' is zero" return None last_item = items[0] if 'owner_id' not in last_item and 'id' not in last_item: print "No 'owner_id' and 'id' in item" return None if last_item['owner_id'] in _BANNED: return None print last_item['owner_id'] print last_item last_item_url = build_wall_url(last_item['owner_id'], last_item['id']) last_item_filename = _LAST_ITEM_FILENAME + "_" + _hashtag last_item_in_file = bot_util.read_one_string_file(last_item_filename) if not last_item_in_file or last_item_url != last_item_in_file: bot_util.write_one_string_file(last_item_filename, last_item_url) print "New post: " + last_item_url print last_item message = "" if 'attachments' in last_item: attachments = last_item['attachments'] photo_url = None for attachment in attachments: if 'photo' in attachment: photo = attachment['photo'] if 'photo_1280' in photo: photo_url = photo['photo_1280'] break if photo_url: print photo_url message += photo_url + "\n\n" owner_id = last_item['owner_id'] if 'text' in last_item: text = last_item['text'] message += text + "\n\n" if not bot_util.check_file_for_string(_ALL_POSTS_FILENAME, last_item_url + "\n"): return None else: bot_util.append_string_to_file(_ALL_POSTS_FILENAME, last_item_url + "\n") message += u"<b>Пост:</b> " + last_item_url + "\n" if owner_id > 0: user_info = get_user_info(owner_id) first_name = user_info['first_name'] last_name = user_info['last_name'] message += u"<b>Автор:</b> <a href=\"https://vk.com/id" + str( owner_id) + u"\">" + first_name + u" " + last_name + u"</a>" else: message += u"<b>Автор:</b> https://vk.com/club" + str(-owner_id) return message else: print "There is no new posts for #" + _hashtag return None
# coding=utf-8 import json import fivethreecyclingbot from broadcast import broadcast_message from util import bot_util import time import os _TOKEN_VK_FILENAME = fivethreecyclingbot.DATA_DIRNAME + 'token_vk' _TOKEN_VK = bot_util.read_one_string_file(_TOKEN_VK_FILENAME) _LAST_ITEM_FILENAME = fivethreecyclingbot.DATA_DIRNAME + 'last_item' _ALL_POSTS_FILENAME = fivethreecyclingbot.DATA_DIRNAME + 'posts' _BANNED_FILENAME = fivethreecyclingbot.DATA_DIRNAME + 'banned' _BANNED = [] if os.path.exists(_BANNED_FILENAME): _BANNED = bot_util.read_lines(_BANNED_FILENAME) _BANNED = [string.rstrip() for string in _BANNED] _BANNED = [string for string in _BANNED if string] print _BANNED _BANNED = map(int, _BANNED) _HASHTAGS = ["53cycling", "novgorodbike"] _53CYCLING_ID = -71413407 _53CYCLING_DOMAIN = "53cycling" def get_newsfeed_search_hashtag_url(hashtag): return "https://api.vk.com/method/newsfeed.search?q=%23{h}&rev=1&v=5.63&access_token={t}".format( t=_TOKEN_VK, h=hashtag)
wind_direction = avewind["dir"] emoji = u"" print icon if icon == "chancerain": emoji = DROPS_EMOJI if icon == "cloudy": emoji = OVERCAST_EMOJI return ( low + u"°C ... " + high + u"°C, " + conditions + u" " + emoji + u", Wind: " + wind_direction + u" " + wind_speed + u" kp/h\n" ) if __name__ == "__main__": f = YandexForecaster.get_forecast() t = bot_util.read_one_string_file("../data/weather_com_token") f = WeatherComForecaster(t) f = f.get_forecast() if f: print "OK"
else: return u"Проблемы при получении прогноза погоды" if self._check_message_for_command(text, self.COMMAND_GET_BUTTHUR): return "https://pp.vk.me/c629309/v629309903/209b8/a22Q1yCTn4s.jpg" return False def _get_start_message(self): return """ Погода в великом Новгороде Команды: /start, /help /getweather — Погода /getforecast — Прогноз Погода — [Новгород.ру](http://novgorod.ru/weather) Прогноз — Яндекс.Погода, Weather.com [Логотип бота](vk.com/mzzaxixart) [Автор бота](ilya.fut33v.ru/contacts), Telegram: @fut33v [Оценить в Store Bot](https://telegram.me/storebot?start=novgorodweatherbot) [Github](https://github.com/fut33v/NovgorodWeatherBot) """ if __name__ == "__main__": t = bot_util.read_one_string_file(TOKEN_FILENAME) botan_t = bot_util.read_one_string_file(BOTAN_TOKEN_FILENAME) weather_com_t = bot_util.read_one_string_file(WEATHER_COM_TOKEN_FILENAME) bot = NovgorodWeatherBot(t, name="NovgorodWeatherBot", botan_token=botan_t, weather_com_token=weather_com_t) bot.start_poll()