forked from shakul12/telegram_twitter_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (42 loc) · 1.41 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
import os
from dotenv import load_dotenv
load_dotenv()
import requests
import json
import tweepy
token= os.getenv('token')
consumer_key= os.getenv('consumer_key')
consumer_secret= os.getenv('consumer_secret')
access_token= os.getenv('access_token')
access_token_secret= os.getenv('access_token_secret')
botsUrl= "https://api.telegram.org/bot{}".format(token)
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
def giveUpdate(offset=None):
url = botsUrl+ "/getupdates?timeout=100"
if offset:
url = botsUrl+ "/getupdates?offset={}&timeout=100".format(offset+1)
resp= requests.get(url)
return json.loads(resp.content)
def sendMessage(msg, chat_id):
url= botsUrl+ "/sendMessage?chat_id={}&text={}".format(chat_id,msg)
resp= requests.get(url)
return "sent message"
def getReply(msg):
tweets= tweepy.Cursor(api.search, q= "#{}".format(msg), lang="en").items(5)
all_tweets= [tw.user.screen_name+ ' - ' + tw.text for tw in tweets]
return all_tweets
id_=None
while True:
update= giveUpdate(offset=id_)
update= update['result']
if update:
for item in update:
id_= item['update_id']
msg= item['message']['text']
chat_id= item['message']['from']['id']
if msg:
reply= getReply(msg)
for tw in reply:
print(sendMessage(tw, chat_id))