Ejemplo n.º 1
0
    def google_url_shortener(self):
        '''
            google短網址
        '''
        if self.key is None:
            return text['google短網址說明']

        self._count({'觸發':1}) #紀錄次數
        #MessageLogs.add(self.group_id, self.user_id, nAIset=1) #紀錄次數

        return '愛醬幫你申請短網址了喵\n%s' % google_shorten_url(self.message.replace('短網址=', ''))
Ejemplo n.º 2
0
    def main(self):
        '''
            關鍵字觸發
        '''
        if 'http:' in self.message or 'https:' in self.message:  #如果內容含有網址 做網址檢查
            if self.group:
                self._count({'網頁': 1})  #紀錄次數
                return google_safe_browsing(self.message)
            else:
                return google_shorten_url(self.message)  #短網址

        self.message = self.message.lower().strip(' \n')  #調整內容 以增加觸發命中率
        self._count({
            '對話': 1,
            '髒話': self.message.count('幹') + self.message.count('f**k'),
            '字數': len(self.message),
        })

        if self.message == '':
            return None

        #暫存訊息 用於對話模型訓練
        #if self.group:
        #    with open('E:\\bot_log\\%s.log' % self.group_id, 'a+', encoding='utf-8') as f:
        #        f.write(self.message + '\n')

        #愛醬開頭可以強制呼叫
        if self.group:
            if self.message != text['名稱'] and self.message[:2] == text['名稱']:
                message_old = self.message
                self.message = self.message[2:].strip(' \n ')
                reply_message = self.check(UserKeyword.get(self.group_id))
                if reply_message:
                    return reply_message

                reply_message = self.check(UserKeyword.get(self.user_id))
                if reply_message:
                    return reply_message
                self.message = message_old

        #睡覺模式
        if UserSettings_temp.has_option(self.group_id, '暫停'):
            if time() > UserSettings_temp.getfloat(self.group_id, '暫停'):
                UserSettings_temp.remove_option(self.group_id, '暫停')
                UserSettings_temp.save()
                return text['睡醒']
        #一般模式
        else:
            if not self.group or (self.user and UserSettings.get(
                    self.group.id, self.user.id, '個人詞庫', False)):  #檢查是否使用個人詞庫
                reply_message = self.check(UserKeyword.get(self.user.id))
                if reply_message:
                    return reply_message

            if self.group and self.user:
                if not UserSettings.get(self.group.id, self.user.id, '別理我',
                                        False):  #檢查不理我模式
                    reply_message = self.check(UserKeyword.get(self.group.id))
                    if reply_message:
                        return reply_message

        #全回應模式
        if self.message[:2] == text['名稱'] or self.group_id is None:
            if self.group is None or UserSettings.get(
                    self.group_id, None, '全回應', default=False):
                if self.message != text['名稱'] and self.message[:2] == text[
                        '名稱']:  #做兩層是為了方便1對1不見得也要愛醬開頭
                    self.message = self.message[2:].strip(' \n ')

                reply_message = self.check(UserKeyword.get(), all_reply=True)
                if reply_message:
                    return reply_message
                if self.group:
                    return choice(text['未知'])
            else:
                return choice(text['未知']) + '\n(全回應模式關閉)\n使用「設定」開啟'

        if self.group_id is None:
            return text['預設回覆']
        else:
            return None
Ejemplo n.º 3
0
def event_main(bot_id, group_id, user_id, message, key, value, **argv):
    #後處理
    def later(reply_message):
        MessageLogs.add(group_id, user_id, nAItrigger=1)  #紀錄次數

        #取參數
        opt = {}
        if '##' in reply_message:
            reply_message_new = []
            for i in reply_message.split('##'):
                if '=' in i:
                    a, *b = i.split('=')
                    opt[a] = '='.join(b)
                else:
                    reply_message_new.append(i)
            reply_message = ''.join(reply_message_new)

        #隨機 (算法:比重)
        if '__' in reply_message:
            weight_total = 0
            result_pool = {}
            minimum_pool = []
            for msg in reply_message.split('__'):
                if msg == '':
                    continue

                index = msg.rfind('%')
                if index > -1 and isFloat(msg[index + 1:].strip()):
                    #if index > -1 and msg[index+1:].strip().isdigit():
                    weight = float(msg[index + 1:].strip())
                    msg = msg[:index]
                else:
                    weight = 1
                weight_total += weight

                is_minimum = msg[:1] == '*'
                if is_minimum:
                    is_minimum_pool = msg[:2] == '**'
                    msg = msg[2:] if is_minimum_pool else msg[1:]

                result_pool[msg] = {
                    'weight': weight,
                    'is_minimum': is_minimum,
                }
                if is_minimum and is_minimum_pool:
                    minimum_pool.append(msg)

            count = int(
                message[message.rfind('*') +
                        1:]) if '*' in message and message[message.rfind('*') +
                                                           1:].isdigit() else 1
            if count > 10000: count = 10000
            if count < 1: count = 1
            if count == 1 and '種子' in opt and opt['種子'].isdigit() and int(
                    opt['種子']) > 0:
                seed_time = int((datetime.now() - datetime(2017, 1, 1)).days *
                                24 / int(opt['種子']))
                seed = int(
                    md5((str(user_id) + str(seed_time)
                         ).encode()).hexdigest().encode(), 16) % weight_total
            else:
                try:
                    #random.org的隨機據說為真隨機
                    if count > 1:
                        r = requests.get(
                            'https://www.random.org/integers/?num=%s&min=0&max=%s&col=1&base=10&format=plain&rnd=new'
                            % (count, int(weight_total)),
                            timeout=3)
                        if 'Error' in r.text:
                            raise
                        seed = r.text.split('\n')[:-1]
                    else:
                        raise
                except:
                    seed = [
                        uniform(0, int(weight_total)) for i in range(count)
                    ]

            minimum_count = 0
            minimum_index = int(opt.get('保底', 10))
            reply_message_new = {}
            reply_message_image = []
            for i in range(count):
                #r = uniform(0, weight_total) if seed == -1 else seed
                r = float(seed[i]) if type(seed) == list else seed
                for msg, msg_opt in result_pool.items():
                    if r > msg_opt['weight']:
                        r -= msg_opt['weight']
                    else:
                        minimum_count = 0 if msg_opt[
                            'is_minimum'] else minimum_count + 1
                        if minimum_count >= minimum_index and len(
                                minimum_pool) > 0:
                            minimum_count = 0
                            msg = choice(minimum_pool)
                        if msg[:6] == 'https:':
                            reply_message_image.append(msg)
                            if len(reply_message_image) > 5:
                                break
                        else:
                            reply_message_new[msg] = (
                                reply_message_new[msg] +
                                1) if msg in reply_message_new else 1
                        break

            if len(reply_message_new) > 0:
                if count == 1:
                    reply_message = list(reply_message_new.keys())
                else:
                    reply_message = []
                    for msg, num in reply_message_new.items():
                        reply_message.append('%s x %s' % (msg, num))
                    reply_message = ['\n'.join(reply_message)]
            else:
                reply_message = []
            reply_message.extend(reply_message_image[:5])

        #這邊有待優化
        if type(reply_message) == str:
            reply_message = [reply_message]
        reply_message_new = []
        for msg in reply_message:
            for msg_split in msg.split('||'):
                reply_message_new.append(msg_split)
        return reply_message_new

    message = message.lower()
    if 'http:' in message or 'https:' in message:  #如果內容含有網址 不觸發 順便紀錄
        MessageLogs.add(group_id, user_id, nUrl=1)  #紀錄次數
        return None
    else:
        MessageLogs.add(group_id,
                        user_id,
                        nText=1,
                        nFuck=(message.count('幹') + message.count('f**k')),
                        nLenght=len(message))  #紀錄次數

    if UserSettings_temp.has_option(group_id, '暫停'):
        if time() > UserSettings_temp.getfloat(group_id, '暫停'):
            UserSettings_temp.remove_option(group_id, '暫停')
            UserSettings_temp.save()
            return '愛醬大復活!'
        else:
            return None

    if group_id is not None:
        reply_message = UserKeyword.get(group_id, message)
        if reply_message is not None:
            return later(reply_message.reply)

    if user_id is not None:
        reply_message = UserKeyword.get(user_id, message)
        if reply_message is not None:
            return later(reply_message.reply)

    keys = []
    if group_id is not None:
        for i in UserKeyword.get(group_id):
            keys.append((i.keyword, i.reply))
    if user_id is not None:
        for i in UserKeyword.get(user_id):
            keys.append((i.keyword, i.reply))

    for k, v in keys:
        kn = -1
        k_arr = k.split('**')
        for k2 in k_arr:
            if k2 != '':
                n = message.find(k2)
                if n > kn:
                    kn = n
                else:
                    break
            #最後檢查前後如果為任意字元的情況 那相對的最前最後一個字元必須相等 雖然使用字串會比較精準 暫時先用一個字元 如果**混在中間有可能誤判 但是問題不大
            if k_arr[0] != '' and message[0] != k[0]: break
            if k_arr[-1] != '' and message[-1] != k[-1]: break
        else:
            return later(v)

    #使用全群組的關鍵字 限無**
    if group_id is not None and UserSettings.get(group_id, 'all_group_keyword',
                                                 False):
        for k in UserKeyword.query.filter_by(
                super=0, keyword=message).order_by(UserKeyword._id.desc()):
            return later(k.reply)

    if group_id is None:
        message = message.strip()
        if message[:4] == 'http':
            return '愛醬幫你申請短網址了喵\n%s' % google_shorten_url(message)
        else:
            return '群組指令說明輸入【指令】\n個人服務:\n直接傳給愛醬網址即可產生短網址\n直接傳圖給愛醬即可上傳到圖床\n其他功能如果有建議請使用回報'
    else:
        return None
Ejemplo n.º 4
0
def event_shorten_url(bot_id, group_id, user_id, message, key, value, **argv):
    MessageLogs.add(group_id, user_id, nAItrigger=1)  #紀錄次數
    return '愛醬幫你申請短網址了喵\n%s' % google_shorten_url(message.replace('短網址=', ''))