Telegram bot is not sending messages - python-telegram-bot

I'm trying to execute this simple telegram bot but I get no output and I don't know what I'm doing wrong. This is the code:
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
def hello(update: Update, context: CallbackContext) -> None:
update.message.reply_text(f'Hello {update.effective_user.first_name}')
updater = Updater('my_token')
updater.dispatcher.add_handler(hello)
updater.start_polling()
updater.idle()

I think you missed some parameters in the add_handler function, so it should be like the following:
updater.dispatcher.add_handler(CommandHandler('hello', hello))

Related

discord.py - message.content returns null

I am currently trying to program my own Discord Bot with Python. I have now made an "on_message" event, but "message.content" is not returning anything. No message, nothing! Can you help me?
#client.event
async def on_message(message):
if message.content.startswith("$test"):
await message.channel.send("test")
You don't have the message_content intent, so you can't read messages.
Docs: https://discordpy.readthedocs.io/en/latest/intents.html
Don't forget to enable them on your dev portal as well.
Also, instead of manually parsing messages with prefixes, consider using the built-in Commands framework that does all this for you. Docs: https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html
PS "null" is not a thing in Python, it's None. And the message content isn't None/"null", it's an empty string ("").

What is the keep_alive library and will it work?

I saw a question for a discord bot and it was using a library called "keep_alive". How can I import it to my bot if it does keep the bot online?
The below code is the keep_alive code which prevents your repl from dying by creating a flask server. You should use uptime-robot to make the server gets pinged in a certain amount of time to make it work for long time without stopping.
from flask import Flask
from threading import Thread
app = Flask('')
#app.route('/')
def main():
return "Your bot is alive!"
def run():
app.run(host="0.0.0.0", port=8080)
def keep_alive():
server = Thread(target=run)
server.start()
You should create a keep_alive and paste the above code there and then add keep_alive.keep_alive() in your main file to make it work.
You can refer this youtube-video to understand how to use that

How do I make my discord bot copy and paste someone's message in python?

I am trying to make a command that has an interaction like this:
User: p.poesia (text) #user
Bot: (text copied)
#user, 2021
Please consider reading the discord.py documentation.
This is the basic code I made for you to run it.
#client.command()
async def poesia(ctx, text):
await ctx.send(text + str(ctx.author))

Command not found error after changing virtual machine

Okay so I have a music bot code that was working before I switched to another virtual machine provider. All the requirements are exactly the same as in my previous virtual machine because I copy and pasted everything including the requirements.txt. The bot runs normally with 0 errors until I try to run any of the commands. It gave me this error:
discord.ext.commands.errors.CommandNotFound: Command "play" is not found
I've tried rolling back to the rewrite version I started the project on,
changed #client.command to #bot.command after assigning bot = commands.Bot(command_prefix='prefix')
#I've assigned client = discord.ext.commands
#client.command(name='play', aliases=['sing'])
async def play(self, ctx, *, search: str):
#then some code
update 1: Ran it as a cog and raised:
discord.ext.commands.errors.ExtensionFailed: Extension 'music' raised an error: TypeError: cogs must derive from Cog
update 2: No idea why rolling back the rewrite version didn't work though. Perhaps I didn't do it correctly.
Just simply run it as a cog.
Note that the way cogs work has been updated recently:
https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html
If you still want to run it as a standalone bot,
your bot should looks like something along the lines of:
from discord.ext.commands import Bot
bot = Bot("!")
#bot.command(name='play', aliases=['sing'])
async def play(ctx, *, search: str): # Note no self
#then some code
bot.run("token")
It's important that the bot that you run is the same bot that you register the commands with. You're also passing self to your bot even though it's not in a cog, which doesn't make sense.
Okay so I found the problem.
The bot doesnt work when I try to run it as a standalone bot.
The reason using it as a cog don't work the first time was because.
the way cogs work has been changed in discord.py rewrite.
These are the changes I made:
#in cogs/music.py
class Music:
#Code
#bot.event
async def on_ready():
print('Music bot online')
to
#in cogs/music.py
class Music(commands.Cog):
#Code
#commands.Cog.listener()
async def on_ready():
print('Music bot online')
Thank you to the legendary #PatrickHaugh for helping me figure this out.

discord.py delete author message after executing command

I need the bot to delete the message from the command author, and leave the bot message. Any help will be appreciated! thank you.
I have already tried looking for a answer on google but nothing has worked
I'm kinda late here. I tried implementing this with the new 1.0 terms but couldn't make it work. If anyone has an updated version, please do tell
Edit: I found that the new best way of doing it is to just add this at the end of the function:
await ctx.message.delete()
No need for a separate delete function anymore.
You can obtain the message that called the command by passing the context with the command using the pass_context option. You can use the Client.delete_message coroutine to delete messages.
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command(pass_context=True)
async def deletethis(ctx):
await bot.say('Command received')
await bot.delete_message(ctx.message)
await bot.say('Message deleted')
bot.run('token')

Resources