Помогите пожалуйста!!!

Izamu_Takahashi

Новичок
Пользователь
Янв 14, 2024
2
0
1
Всем привет, я новичок
Нужно создать бот в телеграмме, который автоматически постит посты из твиттера в телеграмм канал.
Вот прописанный код(логи изменены в целях безопасности)

import tweepy
import logging
from telegram import Bot, ParseMode
from telegram.ext import Updater, CallbackContext
from telegram.ext import JobQueue

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Twitter API credentials
consumer_key = "vdSsOoFfJPSc47tcefV692y"
consumer_secret = "hCsZmDiexpI3Ys1b9lOyT4nR2Vk5VxR1CXA64hPAP7I5bC"
access_token = "9839345669760614-51mLhYMsBvYICad9QJPtLRsSsEqsx"
access_token_secret = "hv23K6AHuyd5k9p5RohrbLFyqGfqCHgYxl481E2y3"

# Twitter OAuth 2.0 credentials
client_id = "UHdOMVctakRsUW0yJaR080b0k6MTpjaQ"
client_secret = "6eoLdA7g7Na9KMb45wKpgxQ7Y0eVjWj560uND_66moKLoE4HZS"

# Telegram Bot Token
telegram_token = "6924420737:AAEGq4sWe8c7KtTUhwTsWQ1jzvyy9cE4"

# Telegram Channel ID
telegram_channel_id = "@SnibbuCrabArmy"

# Authenticate with Twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitter_api = tweepy.API(auth)

# Authenticate with Twitter using OAuth 2.0
auth_2 = tweepy.AppAuthHandler(client_id, client_secret)
twitter_api_2 = tweepy.API(auth_2, wait_on_rate_limit=True)

# Authenticate with Telegram
telegram_bot = Bot(token=telegram_token)

def post_latest_tweet(context: CallbackContext):
logger.info("Posting latest tweet...")
twitter_username = "@snibbucoineth"

try:
user_tweets = twitter_api_2.user_timeline(screen_name=twitter_username, count=1, tweet_mode="extended")
if user_tweets:
latest_tweet = user_tweets[0].full_text
else:
logger.info("No tweets found.")
return
except tweepy.error.TweepError as e:
logger.error(f"Error fetching tweet: {str(e)}")
return

try:
context.bot.send_message(chat_id=telegram_channel_id, text=latest_tweet, parse_mode=ParseMode.HTML)
logger.info("Tweet posted successfully.")
except Exception as e:
logger.error(f"Error posting to Telegram: {str(e)}")

if __name__ == "__main__":
logger.info("Bot is starting...")
updater = Updater(token=telegram_token, use_context=True)
dispatcher = updater.dispatcher

job_queue = updater.job_queue
job_queue.run_repeating(post_latest_tweet, interval=300, first=0)

updater.start_polling()
updater.idle()

Вот код
Выдает ошибку по типу:
Traceback (most recent call last):
File "C:\Users\BLACK\twitter_to_telegram_bot.py", line 3, in <module>
from telegram import Bot, ParseMode
ImportError: cannot import name 'ParseMode' from 'telegram' (C:\Users\BLACK\AppData\Local\Programs\Python\Python312\Lib\site-packages\telegram\__init__.py)
 

4olshoy_blen

Популярный
Пользователь
Ноя 13, 2022
423
115
43
from telegram import Bot
from telegram.constants import ParseMode
 

Форум IT Специалистов