예제 #1
0
파일: app.py 프로젝트: ari-sawali/LineBot
def handle_join(event):
    try:
        reply_token = event.reply_token
        cid = bot.line_api_wrapper.source_channel_id(event.source)
        
        if not bot.line_event_source_type.determine(event.source) == bot.line_event_source_type.USER:
            group_data = db.group_manager(MONGO_DB_URI).get_group_by_id(cid)
            group_action_dict = { '查看群組相關資料': bot.msg_handler.text_msg_handler.CH_HEAD + u'群組的資料' }

            template_alt_text = '群組資料查閱快捷樣板'
            template_title = '相關指令'

            if group_data is None:
                activation_token = global_handler._group_manager.new_data(cid, db.group_data_range.ALL)
                
                group_action_dict['啟用公用資料庫'] = bot.msg_handler.text_msg_handler.CH_HEAD + u'啟用公用資料庫' + activation_token
                group_template = bot.line_api_wrapper.wrap_template_with_action(group_action_dict, template_alt_text, template_title)
                line_api.reply_message(reply_token, 
                                       [bot.line_api_wrapper.introduction_template(),
                                        bot.line_api_wrapper.wrap_text_message('群組資料註冊{}。'.format('成功' if activation_token is not None else '失敗'), webpage_generator),
                                        group_template])
            else:
                group_template = bot.line_api_wrapper.wrap_template_with_action(group_action_dict, template_alt_text, template_title)
                line_api.reply_message(reply_token, 
                                       [bot.line_api_wrapper.introduction_template(),
                                        bot.line_api_wrapper.wrap_text_message('群組資料已存在。', webpage_generator),
                                        group_template])
    except Exception as ex:
        handle_error(event, ex)
예제 #2
0
    def __init__(self, flask_app, config_manager, line_api_wrapper,
                 mongo_client, oxford_api, system_data, webpage_generator,
                 imgur_api_wrapper, oxr_client, string_calculator,
                 weather_reporter, file_tmp_path, rss_data_mgr, last_chat_rec):
        self._flask_app = flask_app
        self._config_manager = config_manager
        self._last_chat_rec = last_chat_rec

        self._array_separator = param_validator.ARRAY_SEPARATOR

        self._system_data = system_data
        self._system_config = db.system_config(mongo_client)
        self._system_stats = db.system_statistics(mongo_client)
        self._stk_rec = db.sticker_recorder(mongo_client)
        self._loop_prev = bot.infinite_loop_preventer(
            self._config_manager.getint(
                bot.config_category.SYSTEM,
                bot.config_category_system.DUPLICATE_CONTENT_BAN_COUNT),
            self._config_manager.getint(
                bot.config_category.SYSTEM,
                bot.config_category_system.UNLOCK_PASSWORD_LENGTH))
        self._db_measurement = db.measurement_data_wrapper()

        self._kwd_public = db.group_dict_manager(
            mongo_client,
            config_manager.getint(
                bot.config_category.KEYWORD_DICT,
                bot.config_category_kw_dict.CREATE_DUPLICATE),
            config_manager.getint(bot.config_category.KEYWORD_DICT,
                                  bot.config_category_kw_dict.REPEAT_CALL))
        self._kwd_global = db.word_dict_global(mongo_client)
        self._group_manager = db.group_manager(mongo_client)
        self._oxford_dict = oxford_api
        self._line_api_wrapper = line_api_wrapper
        self._webpage_generator = webpage_generator
        self._imgur_api_wrapper = imgur_api_wrapper
        self._oxr_client = oxr_client
        self._string_calculator = string_calculator
        self._weather_reporter = weather_reporter
        self._weather_config = db.weather_report_config(mongo_client)
        self._weather_id_reg = tool.weather.weather_reporter.CITY_ID_REGISTRY
        self._sticker_dl = tool.line_sticker_downloader(file_tmp_path)
        self._pli = tool.currency.pypli()
        self._ctyccy = tool.currency.countries_and_currencies()
        self._ppp = tool.currency.ppp_manager()

        self._rss_data_manager = rss_data_mgr

        self._pymongo_client = mongo_client
예제 #3
0
# import imgur API
from imgur import ImgurClient
from imgur.helpers.error import ImgurClientError

# Main initialization
app = Flask(__name__)
handle_pool = ThreadPool(processes=4)

# Databases initialization
import pymongo
MONGO_DB_URI = os.getenv('MONGO_DB_URI', None)
if MONGO_DB_URI is None:
    print 'Define uri of MongoDB (Complete connection string).'
    sys.exit(1)

group_data = db.group_manager(MONGO_DB_URI)

# TODO: group data usage change (Find all ref)

db_query = db_query_manager("postgres", os.environ["DATABASE_URL"], app)
kwd = db.kw_dict_mgr(db_query)
gb = db.group_ban(db_query)
msg_track = db.message_tracker(db_query)

app_root_url = os.getenv('APP_ROOT_URL', None)
if app_root_url is None or app_root_url.startswith('http'):
    print 'Define App Root URL / Remove HTTP protocol of url'
    sys.exit(1)
else:
    app.config.update(SERVER_NAME=app_root_url)