First working version that tweets!

This commit is contained in:
Hemna 2021-10-12 11:23:06 -04:00
parent d743bd7d47
commit e82d01ae01
4 changed files with 120 additions and 56 deletions

View File

@ -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: <your api key here>
apiKey_secret: <your api key secret here>
access_token: <your Twitter app access token>
access_token_secret: <your Twitter app access token secret>
Installation

View File

@ -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

View File

@ -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

View File

@ -1,2 +1,3 @@
pbr
aprsd>=2.2.0
tweepy