我正在尝试创建一个电报机器人,将请求的推文流式传输到用户,组或频道。到目前为止,我已经能够成功地将推文流式传输到我的计算机,但是,当我尝试将推文发送到我的电报机器人时,我只收到一条推文。我能够在我的计算机上查看推文流但我无法通过电报转发每条推文,将第一个推文作为消息。
请看一下我的代码,任何帮助都会有很大的帮助
updater = Updater(token=TOKEN)
jB = updater.job_queue
dispatcher = updater.dispatcher
class Listener(StreamListener):
def on_data(self, data):
# use json to load data
all_data = json.loads(data)
tweet = all_data["text"]
username = all_data["user"]["screen_name"]
tweet_stream = {"name":username, "twit":tweet}
print(tweet_data)
#remove retweets from the tweets
if not all_data["retweeted"] and 'RT @' not in all_data["text"]:
print((username, tweet))
def bot_message(bot, job):
bot.send_message(chat_id=job.context, text=(username, tweet))
def bot_timer(bot, update, job_queue):
bot.send_message(chat_id=update.message.chat_id, text='Tweet
will be sent every 5 seconds')
job_queue.run_repeating(bot_message, 5,
context=update.message.chat_id)
bot_handler=CommandHandler('tweets',bot_timer,pass_job_queue=True)
updater.dispatcher.add_handler(bot_handler)
# *** need to apply the error codes for the error function
def on_error(self, status):
print(status, "this is an error")
return True
# List of followers to track
followers =[ "759251", "1333467482", "2592325530",
"759252279862104064", "1051053836", "884936655437791232",
"862007728956485632","869908314292924416" ]
twitterStream = Stream(auth, Listener())
twitterStream.filter(follow=followers, async=True)
# telegram to send message
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s -
%(message)s', level=logging.INFO)
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
updater.start_polling(timeout=5)