[twitter] Add start, stop, status REST endpoints
This commit is contained in:
parent
fc324f102d
commit
f6eef5dc73
|
@ -7,7 +7,7 @@ from kibicara.config import config
|
||||||
from kibicara.platformapi import Censor, Message, Spawner
|
from kibicara.platformapi import Censor, Message, Spawner
|
||||||
from kibicara.platforms.twitter.model import Twitter
|
from kibicara.platforms.twitter.model import Twitter
|
||||||
from logging import getLogger
|
from logging import getLogger
|
||||||
from peony import PeonyClient
|
from peony import PeonyClient, exceptions
|
||||||
|
|
||||||
|
|
||||||
logger = getLogger(__name__)
|
logger = getLogger(__name__)
|
||||||
|
@ -17,18 +17,21 @@ class TwitterBot(Censor):
|
||||||
def __init__(self, twitter_model):
|
def __init__(self, twitter_model):
|
||||||
super().__init__(twitter_model.hood)
|
super().__init__(twitter_model.hood)
|
||||||
self.twitter_model = twitter_model
|
self.twitter_model = twitter_model
|
||||||
self.client = PeonyClient(
|
self.enabled = self.twitter_model.enabled
|
||||||
consumer_key=config['twitter']['consumer_key'],
|
|
||||||
consumer_secret=config['twitter']['consumer_secret'],
|
|
||||||
access_token=twitter_model.access_token,
|
|
||||||
access_token_secret=twitter_model.access_token_secret,
|
|
||||||
)
|
|
||||||
self.polling_interval_sec = 60
|
self.polling_interval_sec = 60
|
||||||
self.mentions_since_id = self.twitter_model.mentions_since_id
|
self.mentions_since_id = self.twitter_model.mentions_since_id
|
||||||
self.dms_since_id = self.twitter_model.dms_since_id
|
self.dms_since_id = self.twitter_model.dms_since_id
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
if self.twitter_model.successful_verified:
|
try:
|
||||||
|
if not self.twitter_model.verified:
|
||||||
|
raise ValueError('Oauth Handshake not completed')
|
||||||
|
self.client = PeonyClient(
|
||||||
|
consumer_key=config['twitter']['consumer_key'],
|
||||||
|
consumer_secret=config['twitter']['consumer_secret'],
|
||||||
|
access_token=self.twitter_model.access_token,
|
||||||
|
access_token_secret=self.twitter_model.access_token_secret,
|
||||||
|
)
|
||||||
if self.twitter_model.mentions_since_id is None:
|
if self.twitter_model.mentions_since_id is None:
|
||||||
logger.debug('since_id is None in model, fetch newest mention id')
|
logger.debug('since_id is None in model, fetch newest mention id')
|
||||||
await self._poll_mentions()
|
await self._poll_mentions()
|
||||||
|
@ -37,8 +40,16 @@ class TwitterBot(Censor):
|
||||||
await self._poll_direct_messages()
|
await self._poll_direct_messages()
|
||||||
logger.debug('Starting Twitter bot: %s' % self.twitter_model.__dict__)
|
logger.debug('Starting Twitter bot: %s' % self.twitter_model.__dict__)
|
||||||
await gather(self.poll(), self.push())
|
await gather(self.poll(), self.push())
|
||||||
else:
|
except CancelledError:
|
||||||
logger.debug('Twitter Bot not started: Oauth Handshake not completed')
|
logger.debug(f'Bot {self.twitter_model.hood.name} received Cancellation.')
|
||||||
|
raise
|
||||||
|
except exceptions.Unauthorized:
|
||||||
|
logger.debug(f'Bot {self.twitter_model.hood.name} has invalid auth token.')
|
||||||
|
await self.twitter_model.update(enabled=False)
|
||||||
|
self.enabled = self.twitter_model.enabled
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
logger.debug(f'Bot {self.twitter_model.hood.name} stopped.')
|
||||||
|
|
||||||
async def poll(self):
|
async def poll(self):
|
||||||
while True:
|
while True:
|
||||||
|
|
|
@ -13,7 +13,8 @@ class Twitter(Model):
|
||||||
mentions_since_id: Integer(allow_null=True) = None
|
mentions_since_id: Integer(allow_null=True) = None
|
||||||
access_token: Text()
|
access_token: Text()
|
||||||
access_token_secret: Text()
|
access_token_secret: Text()
|
||||||
successful_verified: Boolean() = False
|
verified: Boolean() = False
|
||||||
|
enabled: Boolean() = False
|
||||||
|
|
||||||
class Mapping(Mapping):
|
class Mapping(Mapping):
|
||||||
table_name = 'twitterbots'
|
table_name = 'twitterbots'
|
||||||
|
|
|
@ -43,8 +43,30 @@ async def twitter_delete(twitter=Depends(get_twitter)):
|
||||||
await twitter.delete()
|
await twitter.delete()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/{twitter_id}/status', status_code=status.HTTP_200_OK)
|
||||||
|
async def twitter_status(twitter=Depends(get_twitter)):
|
||||||
|
return {'status': spawner.get(twitter).status.name}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post('/{twitter_id}/start', status_code=status.HTTP_200_OK)
|
||||||
|
async def twitter_start(twitter=Depends(get_twitter)):
|
||||||
|
await twitter.update(enabled=True)
|
||||||
|
spawner.get(twitter).start()
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post('/{twitter_id}/stop', status_code=status.HTTP_200_OK)
|
||||||
|
async def twitter_stop(twitter=Depends(get_twitter)):
|
||||||
|
await twitter.update(enabled=False)
|
||||||
|
spawner.get(twitter).stop()
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
@router.post('/', status_code=status.HTTP_201_CREATED)
|
@router.post('/', status_code=status.HTTP_201_CREATED)
|
||||||
async def twitter_create(response: Response, hood=Depends(get_hood)):
|
async def twitter_create(response: Response, hood=Depends(get_hood)):
|
||||||
|
"""
|
||||||
|
`https://api.twitter.com/oauth/authorize?oauth_token=`
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
request_token = await get_oauth_token(
|
request_token = await get_oauth_token(
|
||||||
config['twitter']['consumer_key'],
|
config['twitter']['consumer_key'],
|
||||||
|
@ -78,7 +100,8 @@ async def twitter_read_callback(oauth_token: str, oauth_verifier: str):
|
||||||
await twitter.update(
|
await twitter.update(
|
||||||
access_token=access_token['oauth_token'],
|
access_token=access_token['oauth_token'],
|
||||||
access_token_secret=access_token['oauth_token_secret'],
|
access_token_secret=access_token['oauth_token_secret'],
|
||||||
successful_verified=True,
|
verified=True,
|
||||||
|
enabled=True,
|
||||||
)
|
)
|
||||||
spawner.start(twitter)
|
spawner.start(twitter)
|
||||||
return []
|
return []
|
||||||
|
|
Loading…
Reference in a new issue