diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..b5a0d40 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: python +python: + - "3.6" +install: + - pip install -r requirements.txt +script: + - python -m unittest discover diff --git a/nicobot/__init__.py b/nicobot/__init__.py index 9165f6e..a6078f3 100644 --- a/nicobot/__init__.py +++ b/nicobot/__init__.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- -from bot import Bot -from chatter import Chatter -from helpers import * +from .bot import Bot +from .chatter import Chatter +from .helpers import * diff --git a/nicobot/askbot.py b/nicobot/askbot.py index 7dfe9e8..88069b4 100644 --- a/nicobot/askbot.py +++ b/nicobot/askbot.py @@ -18,15 +18,15 @@ import yaml import urllib.request # Own classes -from helpers import * -from bot import Bot -from bot import ArgsHelper as BotArgsHelper -from console import ConsoleChatter -from jabber import JabberChatter -from jabber import arg_parser as jabber_arg_parser -from signalcli import SignalChatter -from signalcli import ArgsHelper as SignalArgsHelper -from stealth import StealthChatter +from .helpers import * +from .bot import Bot +from .bot import ArgsHelper as BotArgsHelper +from .console import ConsoleChatter +from .jabber import JabberChatter +from .jabber import arg_parser as jabber_arg_parser +from .signalcli import SignalChatter +from .signalcli import ArgsHelper as SignalArgsHelper +from .stealth import StealthChatter # Default configuration (some defaults still need to be set up after command line has been parsed) @@ -55,7 +55,7 @@ class AskBot(Bot): patterns : a list of 2-element list/tuple as [name,pattern] """ - def __init__( self, chatter, message, output=sys.stdout, err=sys.stderr, patterns=[], max_count=-1 ): + def __init__( self, chatter, message, patterns=[], max_count=-1 ): # TODO Implement a global session timeout after which the bot exits self.status = { @@ -66,8 +66,6 @@ class AskBot(Bot): self.chatter = chatter self.message = message - self.output = output - self.err = err self.max_count = max_count self.patterns = [] for pattern in patterns: diff --git a/nicobot/bot.py b/nicobot/bot.py index 4d0d085..3257a62 100644 --- a/nicobot/bot.py +++ b/nicobot/bot.py @@ -8,10 +8,10 @@ import signal import sys -from console import ConsoleChatter -from jabber import JabberChatter -from signalcli import SignalChatter -from stealth import StealthChatter +from .console import ConsoleChatter +from .jabber import JabberChatter +from .signalcli import SignalChatter +from .stealth import StealthChatter class Bot: @@ -162,7 +162,7 @@ class ArgsHelper: # By default (or if backend == "console"), will read from stdin or a given file and output to console else: logging.debug("Console backend selected") - chatter = ConsoleChatter(args.input_file,sys.stdout) + chatter = ConsoleChatter(args.input_file) if args.stealth: chatter = StealthChatter(chatter) diff --git a/nicobot/console.py b/nicobot/console.py index f8f115a..f8f300b 100644 --- a/nicobot/console.py +++ b/nicobot/console.py @@ -2,17 +2,17 @@ import logging import sys -from chatter import Chatter + +from .chatter import Chatter class ConsoleChatter(Chatter): """ - Bot engine that reads from a stream and outputs to another + Bot engine that reads messages from a stream (stdin by default) """ - def __init__( self, input=sys.stdin, output=sys.stdout ): + def __init__( self, input=sys.stdin ): self.input = input - self.output = output self.exit = False def start( self, bot ): diff --git a/nicobot/jabber.py b/nicobot/jabber.py index 0a43d72..03038ed 100755 --- a/nicobot/jabber.py +++ b/nicobot/jabber.py @@ -15,8 +15,8 @@ from slixmpp_omemo import UndecidedException, UntrustedException, NoAvailableSes from omemo.exceptions import MissingBundleException # Own classes -from chatter import Chatter -from helpers import * +from .chatter import Chatter +from .helpers import * @@ -311,8 +311,9 @@ class JabberChatter(Chatter): Sends the given message using the underlying implemented chat protocol """ logging.debug(">>> %s",message) - loop = asyncio.get_event_loop() - loop.run_until_complete(self.xmpp.encrypted_send( body=message, recipient=self.recipient )) + #loop = asyncio.get_event_loop() + # loop.run_until_complete(self.xmpp.encrypted_send( body=message, recipient=self.recipient )) + asyncio.ensure_future( self.xmpp.encrypted_send( body=message, recipient=self.recipient ) ) def stop( self ): """ diff --git a/nicobot/signalcli.py b/nicobot/signalcli.py index cc65fef..c0d2aec 100755 --- a/nicobot/signalcli.py +++ b/nicobot/signalcli.py @@ -15,8 +15,8 @@ import subprocess import sys import time -from chatter import Chatter -from helpers import * +from .chatter import Chatter +from .helpers import * # Generic timeout for signal-cli commands to return (actually only 'send' because 'receive' uses its own timeout) diff --git a/nicobot/stealth.py b/nicobot/stealth.py index 1a797ba..28fcb8d 100644 --- a/nicobot/stealth.py +++ b/nicobot/stealth.py @@ -2,7 +2,8 @@ import logging import sys -from chatter import Chatter + +from .chatter import Chatter class StealthChatter(Chatter): diff --git a/nicobot/transbot.py b/nicobot/transbot.py index d5cc77d..4b5c977 100755 --- a/nicobot/transbot.py +++ b/nicobot/transbot.py @@ -22,15 +22,15 @@ import yaml import urllib.request # Own classes -from helpers import * -from bot import Bot -from bot import ArgsHelper as BotArgsHelper -from console import ConsoleChatter -from jabber import JabberChatter -from jabber import arg_parser as jabber_arg_parser -from signalcli import SignalChatter -from signalcli import ArgsHelper as SignalArgsHelper -from stealth import StealthChatter +from .helpers import * +from .bot import Bot +from .bot import ArgsHelper as BotArgsHelper +from .console import ConsoleChatter +from .jabber import JabberChatter +from .jabber import arg_parser as jabber_arg_parser +from .signalcli import SignalChatter +from .signalcli import ArgsHelper as SignalArgsHelper +from .stealth import StealthChatter diff --git a/test_askbot.py b/test_askbot.py new file mode 100644 index 0000000..581e5f5 --- /dev/null +++ b/test_askbot.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import unittest + +from nicobot.askbot import AskBot +from nicobot.console import ConsoleChatter + + +class TestAskbot(unittest.TestCase): + + def test_end_on_yes(self): + bot = AskBot( + chatter = ConsoleChatter( input=["Yes !"] ), + message = "ça va ?", + patterns=[ + [ "yes", r'(?i)\b(yes|ok)\b' ], + [ "no", r'(?i)\bno\b' ], + [ "cancel", r'(?i)\b(cancel|abort)\b' ] + ], + max_count=-1 + ) + result = bot.run() + expected = {'max_count': False, 'events': [{'message': 'Yes !', 'matched_patterns': ['yes']}]} + self.assertEqual(expected,result) + + + def test_dont_end_on_coucou(self): + bot = AskBot( + chatter = ConsoleChatter( input=["Coucou","Yes !"] ), + message = "ça va ?", + patterns=[ + [ "yes", r'(?i)\b(yes|ok)\b' ], + [ "no", r'(?i)\bno\b' ], + [ "cancel", r'(?i)\b(cancel|abort)\b' ] + ], + max_count=-1 + ) + result = bot.run() + expected = {'max_count': False, 'events': [{'message': 'Coucou', 'matched_patterns': []}, {'message': 'Yes !', 'matched_patterns': ['yes']}]} + self.assertEqual(expected,result) + + +if __name__ == '__main__': + unittest.main()