python-zulip-api/zulip_bots/zulip_bots/bots/twitpost/twitpost.py
Anders Kaseorg 2f6623bbc4 cleanup: Remove unused imports.
Generated by autoflake.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-18 20:31:14 -07:00

54 lines
2 KiB
Python

import tweepy
from typing import Any, Dict
class TwitpostBot:
def usage(self) -> str:
return ''' This bot posts on twitter from zulip chat itself.
Use '@twitpost help' to get more information
on the bot usage. '''
help_content = "*Help for Twitter-post bot* :twitter: : \n\n"\
"The bot tweets on twitter when message starts "\
"with @twitpost.\n\n"\
"`@twitpost tweet <content>` will tweet on twitter " \
"with given `<content>`.\n" \
"Example:\n" \
" * @twitpost tweet hey batman\n"
def initialize(self, bot_handler: Any) -> None:
self.config_info = bot_handler.get_config_info('twitter')
auth = tweepy.OAuthHandler(self.config_info['consumer_key'],
self.config_info['consumer_secret'])
auth.set_access_token(self.config_info['access_token'],
self.config_info['access_token_secret'])
self.api = tweepy.API(auth, parser=tweepy.parsers.JSONParser())
def handle_message(self, message: Dict[str, str], bot_handler: Any) -> None:
content = message["content"]
if content.strip() == '':
bot_handler.send_reply(message, 'Please check help for usage.')
return
if content.strip() == 'help':
bot_handler.send_reply(message, self.help_content)
return
content = content.split()
if len(content) > 1 and content[0] == "tweet":
status = self.post(" ".join(content[1:]))
screen_name = status["user"]["screen_name"]
id_str = status["id_str"]
bot_reply = "https://twitter.com/{}/status/{}".format(screen_name,
id_str)
bot_reply = "Tweet Posted\n" + bot_reply
bot_handler.send_reply(message, bot_reply)
def post(self, text):
return self.api.update_status(text)
handler_class = TwitpostBot