Telegram Bot for forwarding telegram messages and tweets help

Hi everyone! I’m new to python and only copy pasted this code from ChatGPT. My goal is to create a telegram bot that will forward messages from a specific telegram channel and tweets from twitter. The error I’m getting from the code is

ImportError                               Traceback (most recent call last)
<ipython-input-12-4b2f56a8c641> in <cell line: 3>()
      1 get_ipython().system('pip install python-telegram-bot')
      2 from tweepy import Client
----> 3 from telegram import Update,Dispatcher
      4 from telegram.ext import Updater, CommandHandler, MessageHandler, filters
      5 from telegram.constants import ParseMode

ImportError: cannot import name 'Dispatcher' from 'telegram' (/usr/local/lib/python3.10/dist-packages/telegram/__init__.py)

Here is the code

!pip install python-telegram-bot
from tweepy import Client
from telegram import Update,Dispatcher
from telegram.ext import Updater, CommandHandler, MessageHandler, filters
from telegram.constants import ParseMode
import logging


# Set up logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)

# Twitter API credentials
consumer_key = 'A'
consumer_secret = 'B'
access_token = 'C'
access_token_secret = 'D'

# Telegram bot token
telegram_token = 'E'

# Telegram channel ID
telegram_channel_id = -1001542659551  # Replace with your channel ID

# Twitter authentication
bearer_token = 'F'
client = Client(bearer_token=bearer_token)
api = Client(consumer_key, consumer_secret, access_token, access_token_secret)

# Telegram bot functions
def start(update, context):
    update.message.reply_text('Welcome to Twitter and Telegram Forwarder Bot! Send me a Twitter username or a Telegram channel message to forward.')

def echo(update, context):
    update.message.reply_text('I can only forward tweets from Twitter accounts and messages from the specified Telegram channel. Send me a Twitter username or a Telegram channel message.')

def forward_tweets(update, context):
    username = update.message.text.strip()
    try:
        tweets = api.user_timeline(screen_name=username, count=5, tweet_mode="extended")
        for tweet in tweets:
            context.bot.send_message(update.message.chat_id, tweet.full_text)
    except tweepy.TweepError as e:
        update.message.reply_text(f"Error: {str(e)}")

def forward_channel_messages(update, context):
    message = update.message
    context.bot.forward_message(chat_id=message.chat_id, from_chat_id=message.forward_from_chat.id, message_id=message.message_id)


# Initialize Telegram bot
updater = Updater(telegram_token,update_queue=None)

# Access the dispatcher property
dp = Dispatcher(updater, queue=None)

# Register handlers
dp.add_handler(CommandHandler("start", start))
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, forward_tweets))
dp.add_handler(MessageHandler(Filters.forwarded & Filters.chat(chat_id=telegram_channel_id), forward_channel_messages))
dp.add_handler(MessageHandler(Filters.all & ~Filters.command, echo))

# Start the Bot
updater.start_polling()
updater.idle()

Looks like ChatGPT is using an older api. See this thread: python - AttributeError: 'Updater' object has no attribute 'dispatcher' - Stack Overflow

or just follow directions at: https://python-telegram-bot.org/

1 Like

Then you have already gone wrong. You wouldn’t try to learn a human language by asking a native speaker to translate some stuff for you, right? That makes no sense; you have no reference for what you’re looking at, and thus no way to understand it. ChatGPT is worse because it has no understanding of what it outputs, no proper independent thought, and no conscious desire to help you. It only generates text that matches what the model says should come next after your prompt. AI is not real.

Thank you Andy!

I will be participating on a Python basics boot camp over the next few weeks as I want to learn programming fyi. I just tried AI as I was curious and I had an idea in mind. Thanks for educating me about AI but man your reply was more toxic than helpful. I was already judged for just using an AI. Good thing the reply prior was helpful. Hope you have a good day man.