-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_telegram_mirror.py
78 lines (61 loc) · 2.91 KB
/
twitter_telegram_mirror.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
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
from twitter_scraper import get_tweets
#import os.path
import os
import configparser
import requests
import ast
__location__ = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__)))
debugPrints = False
config = configparser.ConfigParser()
config.optionxform=str
config.read(os.path.join(__location__, "settings.ini"))
teleBotSettings = dict(config.items('BOT'))
chatIDs = ast.literal_eval(config.get("BOT","CHAT_ID"))
#don't send tweets on the first run
publish = False
#if we have sent tweets before check where we got to
if os.path.exists(os.path.join(__location__, 'lastPublished.txt')):
f = open(os.path.join(__location__, "lastPublished.txt"), "r")
tweetID = (f.read())
publish = True
#load all recent tweets into a list
tweets = []
for tweet in get_tweets(teleBotSettings["TWITTER_HANDLE"], pages=1):
tweets.append(tweet)
#grab the ID of the latest tweet
latestID = tweets[0]['tweetId']
if publish:
#loop across the tweets until we are upto date
for tweet in tweets:
if int(tweet['tweetId']) <= int(tweetID):
break
isRetweet = tweet['isRetweet']
if not (teleBotSettings["ALLOW_RETWEETS"] != 'yes' and isRetweet):
tweetOrRetweet = "Tweet"
if isRetweet:
tweetOrRetweet = "Retweet"
messageToSendToBot="New " + tweetOrRetweet + " from " + teleBotSettings["TWITTER_HANDLE"] +" " + str(tweet['time']) + "%0A" \
"Tweet URL: www.twitter.com" + tweet['tweetUrl'] + "%0A%0A" \
+ tweet['text']
for chatID in chatIDs:
botMessage = 'https://api.telegram.org/bot' + teleBotSettings["BOT_KEY"] + '/sendMessage?chat_id=' + str(chatID) + '&text=' + messageToSendToBot
response = requests.get(botMessage)
jsonResponse = response.json()
if jsonResponse['ok'] != True:
print("FAILED TO SEND TWEETID = " + tweet['tweetId'])
if(debugPrints):
print("Tweet ID: " + tweet['tweetId'] + "\nbody: " + tweet['text'] + "\ntweetURL: www.twitter.com" + tweet['tweetUrl'] + '\n')
else:
messageToSendToBot = "Hello I'm the twitter bot for www.twitter.com/" + teleBotSettings["TWITTER_HANDLE"] + " I will keep this channel updated with all their latest tweets %0A%0AFind out what makes me tick here: https://github.com/Andrew-Pohl/Twitter_telegram_mirror"
for chatID in chatIDs:
botMessage = 'https://api.telegram.org/bot' + teleBotSettings["BOT_KEY"] + '/sendMessage?chat_id=' + str(chatID) + '&text=' + messageToSendToBot
response = requests.get(botMessage)
jsonResponse = response.json()
if jsonResponse['ok'] != True:
print("FAILED TO WELCOME MESSAGE")
#update the file with the new postion
f = open(os.path.join(__location__, "lastPublished.txt"), "w")
f.write(latestID)
f.close()