forked from franciscod/telegram-twitter-forwarder-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
70 lines (56 loc) · 2.55 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import logging
import tweepy
from envparse import Env
from telegram.ext import CommandHandler
from telegram.ext import Updater
from telegram.ext.messagehandler import MessageHandler, Filters
from bot import TwitterForwarderBot
from commands import *
from job import FetchAndSendTweetsJob
env = Env(
TWITTER_CONSUMER_KEY=str,
TWITTER_CONSUMER_SECRET=str,
TWITTER_ACCESS_TOKEN=str,
TWITTER_ACCESS_TOKEN_SECRET=str,
TELEGRAM_BOT_TOKEN=str,
)
if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.WARNING)
logging.getLogger(TwitterForwarderBot.__name__).setLevel(logging.DEBUG)
logging.getLogger(FetchAndSendTweetsJob.__name__).setLevel(logging.DEBUG)
# initialize Twitter API
auth = tweepy.OAuthHandler(env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'))
try:
auth.set_access_token(env('TWITTER_ACCESS_TOKEN'), env('TWITTER_ACCESS_TOKEN_SECRET'))
except KeyError:
print("Either TWITTER_ACCESS_TOKEN or TWITTER_ACCESS_TOKEN_SECRET "
"environment variables are missing. "
"Tweepy will be initialized in 'app-only' mode")
twapi = tweepy.API(auth)
# initialize telegram API
token = env('TELEGRAM_BOT_TOKEN')
updater = Updater(bot=TwitterForwarderBot(token, twapi))
dispatcher = updater.dispatcher
# set commands
dispatcher.add_handler(CommandHandler('start', cmd_start))
dispatcher.add_handler(CommandHandler('help', cmd_help))
dispatcher.add_handler(CommandHandler('ping', cmd_ping))
dispatcher.add_handler(CommandHandler('sub', cmd_sub, pass_args=True))
dispatcher.add_handler(CommandHandler('unsub', cmd_unsub, pass_args=True))
dispatcher.add_handler(CommandHandler('list', cmd_list))
dispatcher.add_handler(CommandHandler('export', cmd_export))
dispatcher.add_handler(CommandHandler('all', cmd_all))
dispatcher.add_handler(CommandHandler('wipe', cmd_wipe))
dispatcher.add_handler(CommandHandler('source', cmd_source))
dispatcher.add_handler(CommandHandler('auth', cmd_get_auth_url))
dispatcher.add_handler(CommandHandler('verify', cmd_verify, pass_args=True))
dispatcher.add_handler(CommandHandler('export_friends', cmd_export_friends))
dispatcher.add_handler(CommandHandler('set_timezone', cmd_set_timezone, pass_args=True))
dispatcher.add_handler(MessageHandler([Filters.text], handle_chat))
# put job
queue = updater.job_queue
queue.put(FetchAndSendTweetsJob(), next_t=0)
# poll
updater.start_polling()