diff --git a/README.rst b/README.rst index a63f4fc..61c5f4f 100644 --- a/README.rst +++ b/README.rst @@ -36,13 +36,30 @@ Send tweet via Ham RADIO! Features -------- -* TODO +* Sent a tweet from your personal twitter account! +* to tweet send a message of "t Hello World #aprs #hamradio" Requirements ------------ -* TODO +* This plugin requires you have a twitter account and create a developer + account with: +* api key +* api key secret +* access token +* access token secret + +Add the following entries to the aprsd.yml file + +.. code:: yaml + + services: + twitter: + apiKey: + apiKey_secret: + access_token: + access_token_secret: Installation diff --git a/aprsd_twitter_plugin/aprsd_twitter_plugin.py b/aprsd_twitter_plugin/aprsd_twitter_plugin.py deleted file mode 100644 index 357e907..0000000 --- a/aprsd_twitter_plugin/aprsd_twitter_plugin.py +++ /dev/null @@ -1,54 +0,0 @@ -import logging - -from aprsd import messaging, plugin, trace - - -LOG = logging.getLogger("APRSD") - - -class SendTweetPlugin(plugin.APRSDRegexCommandPluginBase): - - version = "1.0" - # Look for any command that starts with w or W - command_regex = "^[wW]" - # the command is for ? - command_name = "weather" - - enabled = False - - def setup(self): - # Do some checks here? - self.enabled = True - - def create_threads(self): - """This allows you to create and return a custom APRSDThread object. - - Create a child of the aprsd.threads.APRSDThread object and return it - It will automatically get started. - - You can see an example of one here: - https://github.com/craigerl/aprsd/blob/master/aprsd/threads.py#L141 - """ - if self.enabled: - # You can create a background APRSDThread object here - # Just return it for example: - # https://github.com/hemna/aprsd-weewx-plugin/blob/master/aprsd_weewx_plugin/aprsd_weewx_plugin.py#L42-L50 - # - return [] - - @trace.trace - def process(self, packet): - - """This is called when a received packet matches self.command_regex.""" - - LOG.info("SendTweetPlugin Plugin") - - packet.get("from") - packet.get("message_text", None) - - if self.enabled: - # Now we can process - return "some reply message" - else: - LOG.warning("SendTweetPlugin is disabled.") - return messaging.NULL diff --git a/aprsd_twitter_plugin/twitter.py b/aprsd_twitter_plugin/twitter.py new file mode 100644 index 0000000..652090c --- /dev/null +++ b/aprsd_twitter_plugin/twitter.py @@ -0,0 +1,100 @@ +import logging + +import tweepy +from aprsd import messaging, plugin, trace + + +LOG = logging.getLogger("APRSD") + + +class SendTweetPlugin(plugin.APRSDRegexCommandPluginBase): + + version = "1.0" + # Look for any command that starts with w or W + command_regex = "^[tT]" + # the command is for ? + command_name = "tweet" + + enabled = False + + def setup(self): + # Do some checks here? + self.enabled = True + + # Ensure the access token exists. + if not self.config.exists("services.twitter.apiKey"): + LOG.error("No services.twitter.apiKey exists. Plugin Disabled.") + self.enabled = False + + if not self.config.exists("services.twitter.apiKey_secret"): + LOG.error("No services.twitter.apiKey_secret exists. Plugin Disabled.") + self.enabled = False + + if not self.config.exists("services.twitter.access_token"): + LOG.error("No services.twitter.access_token exists. Plugin Disabled.") + self.enabled = False + + if not self.config.exists("services.twitter.access_token_secret"): + LOG.error("No services.twitter.access_token_secret exists. Plugin Disabled.") + self.enabled = False + + def _create_client(self): + """Create the twitter client object.""" + auth = tweepy.OAuthHandler( + self.config.get("services.twitter.apiKey"), + self.config.get("services.twitter.apiKey_secret"), + ) + + auth.set_access_token( + self.config.get("services.twitter.access_token"), + self.config.get("services.twitter.access_token_secret"), + ) + + api = tweepy.API( + auth, + wait_on_rate_limit=True, + ) + + try: + api.verify_credentials() + LOG.debug("Logged in to Twitter Authentication OK") + except Exception as ex: + LOG.error("Failed to auth to Twitter") + LOG.exception(ex) + return None + + return api + + @trace.trace + def process(self, packet): + + """This is called when a received packet matches self.command_regex.""" + + LOG.info("SendTweetPlugin Plugin") + + from_callsign = packet.get("from") + message = packet.get("message_text", None) + message = message.split(" ") + del message[0] + message = " ".join(message) + + if self.enabled: + # Now we can process + mycall = self.config["ham"]["callsign"] + + # Only allow the owner of aprsd to send a tweet + if not from_callsign.startswith(mycall): + return "Unauthorized" + + client = self._create_client() + if not client: + LOG.error("No twitter client!!") + return "Failed to Auth" + + # Now lets tweet! + client.update_status(message) + + return "Tweet sent!" + else: + LOG.warning("SendTweetPlugin is disabled.") + return messaging.NULL_MESSAGE diff --git a/requirements.txt b/requirements.txt index 3c8784e..f840b21 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pbr aprsd>=2.2.0 +tweepy