telegram bot send_message() hangs with HTML text - python

I am new to Telegram Bot API (python telegram.ext), here is my question: I am trying to send formatted message in reply to received message. For simplicity I removed everything from the code below
mtxt = "<ul><li>line 1</li></ul>"
res = bot.send_message(chat_id=update.message.chat_id, text=mtxt, parse_mode='HTML')
print(res)
Nothing is returned to telegram (no answer from bot) and print() never happens. If I remove 'parse_mode...' clause from the call it works.
I must be fundamentally wrong somewhere... This is very basic staff, what is missing?

There only have limited tags :(
You can use emoji to format, or make a suggestion to Telegram.

Related

"ValueError: Could not find the input entity for PeerChannel" with Bot

For the life of me, I can't figure out this one out.
I have created a new Telegram Bot and created a new channel, in which I added my bot as an Admin.
After reading 100 times the docs, I tried to have the entity "seen" somehow but :
get_dialogs() is not allowed for bots
client.get_entity('') is not allowed for bots
Not sure what else to do…
I do have published some messages in the channel.
My code looks something like :
from telethon import TelegramClient
telethon_client = TelegramClient(
api_id=int(config['TELETHON_API_ID']),
api_hash=config['TELETHON_API_HASH'],
session=config['TELETHON_SESSION']
).start(bot_token=config['TELEGRAM_BOT_TOKEN'])
with telethon_client:
telethon_client.loop.run_until_complete(__async_get_users(chat_id))
async def __async_get_users(chat_id):
channel = await telethon_client.get_entity(chat_id) # -100xxxxx
tg_users = await telethon_client.get_participants(channel)
Any help, lead, or idea appreciated !
Ok, so, not sure what lead to the resolution of this issue but I did :
disable group privacy mode in #BotFather as #Lonami advised
Sent other messages in the group
Removed and re-added the bot to the group
I still had the issue, but after clearing the session, it now works OK!
Thanks #Lonami for the help!

Is there a way that I could see if the message is a reply?

https://i.stack.imgur.com/TjYAZ.png (sorry I cannot post images yet)
Is there a way that I could see if the message is a reply?
In this case, it would get the two emojis. All I would need to know is the message body, the two emojis, and the message it is replying to, "day 72 of smug disliking encrypt".
I can normally just do:
channel = ctx.channel
messages = await channel.history(limit=200).flatten()
However this gets all messages instead of just the reply's. So maybe I would be able to get the replies another way?
You need to use Message.reference
# loop over messages
if message.reference: # if is not None its a reply
...

discord py - message.mentions "else" makes nothing

I want to get the user that was mentioned in a message and send him a private message. That's working without problems, but now I want to add a message for the case, that the mentioned member is not on the same server.
I searched a lot and try now for over 3 hours to find a solution.
Showcase of the problem: https://youtu.be/PYZMVXYtxpE
Heres my code:
#bot.event
async def on_message(message):
if len(message.mentions) == 1:
membe1 = message.mentions[0].id
membe2 = bot.get_user(membe1)
guild = bot.get_guild(message.author.guild.id)
if guild.get_member(membe1) is not None:
await membe2.send(content=f"you was mentioned in the server chat")
else:
embed2 = discord.Embed(title=f"» :warning: | PING not possible", description=f"not possible")
await message.channel.send(content=f"{message.author.mention}", embed=embed)
await message.delete()
return
The first part is working without problems, but at the "else" part, does the bot nothing. He still posts the message with the "invalid" ping in the chat and just ignores it. What can I do to fix that?
There is an error of design in what you are trying to do.
In Discord, you are not able to mention a user if he is not on that same server, so what you are checking will never work at all, currently your code is pretty much checking if the mentioned user exists, which will always happen unless he leaves the guild at the same time the command is executed.
Say for example, to make your command work you want to mention the user user123, who is in your server, you do the following:
#user123 blablabla
And that works because Discord internal cache can find user123 in your server and it is mentioned, which means that clicking on "#user123" will show us a target with his avatar, his roles or whatsoever.
But if you try to do the same for an invalid user, let's say notuser321:
#notuser321 blablabla
This is not a mention, take for example that you know who notuser321 is but he is in the same server as the bot is. Discord cannot retrieve this user from the cache and it is not considered a mention, just a single string, so not a single part of your code will be triggered, because len(message.mentions) will be 0.
Something that you could try would be using regular expressions to find an attempt to tag within message.content. An example would be something like this:
import re
message = "I love #user1 and #user2"
mention_attempt = re.findall(r'[#]\S*', message) # ['#user1', '#user2']

How to simply send a message using discord.py

Okay i have a very simple question but i couldnt find out how to solve it by any chance.
I have a script that fetches some numbers out of the steam website, then compares them. i want to use discord.py to send me that results on discord. i already have the bot on discord, but i coulndt figure out how to just send a message and not react to a message sent by a user or anything. This is my code:
Float = float(Float[8:])
if Float <= 0.01:
element2 = driver.find_element_by_xpath('/html/body/div[1]/div[7]/div[2]/div[2]/div[4]/div[1]/div[3]/div[4]/div[4]/div[2]/div[2]/div[2]/span/span[1]')
Price = element2.text
print("Snipe found: \n"
"Mag-7 Carbon Fiber Factory New with Float:\n"
, Float, '\n', Price)
i also have some selenium in it, but that is not a problem. the variable "Float" is also defined before. i simply want to send the stuff that is in the print function as a message to a discord server.
is this possible by any chance?
Thanks for the help
Just make it a command and use ctx.send(). Since you don't know how to send a message on discord.py, I'm assuming that you are very new to it.
import ...
from discord.ext import commands
import discord
bot = commands.Bot(command_prefix='!')
#bot.command()
async def steamfetch(ctx):
Float = float(Float[8:])
if Float <= 0.01:
element2 = driver.find_element_by_xpath('/html/body/div[1]/div[7]/div[2]/div[2]/div[4]/div[1]/div[3]/div[4]/div[4]/div[2]/div[2]/div[2]/span/span[1]')
Price = element2.text
await ctx.send(f"Snipe found:\nMag-7 Carbon Fiber Factory New with Float:\n{Float}\n{Price}')
You should really look at the discord.py docs or other discord.py tutorials for this because sending a message are in MANY of the discord.py tutorials. I don't know if you did but please try doing your own research before asking a question here.

Client-Side API for Discord (Python)

Is there a Python Client-Side API for Discord?
I don't need much, just to listen to events like getting a call or a message.
Is it possible?
Note that selfbots are against TOS, and you could be banned without warning.
Sounds like you want a selfbot.
What you might be looking for is Discord.py, many selfbots are written in that, such as:
https://github.com/appu1232/Discord-Selfbot
If you would rather not get banned, discord.py is still good for scripting bots for servers.
Ok late answer but maybe someone can benefit from it so here goes.
Never use discord.py for selfbots. Discord.py was created to work on bot accounts not user accounts. That being said, a lot of things in discord.py will flag your account.
If you want, you can use what I'm currently developing with Merubokkusu: discum: discord selfbot api wrapper
Here's the classic ping-pong example:
import discum
bot=discum.Client(token=yourtoken)
#bot.gateway.command
def pingpong(resp):
if resp.event.message:
m = resp.parsed.auto()
if m['content'] == 'ping':
bot.sendMessage(m['channel_id'], 'pong')
bot.gateway.run()
Here's a ping-pong example where you don't reply to yourself:
import discum
bot=discum.Client(token=yourtoken)
#bot.gateway.command
def pingpong(resp):
if resp.event.message:
m = resp.parsed.auto()
if m['author']['id'] != bot.gateway.session.user['id']
if m['content'] == 'ping':
bot.sendMessage(m['channel_id'], 'pong')
bot.gateway.run()
here's another example, this one appends live messages to a list:
import discum
bot=discum.Client(token=yourtoken)
messagelist = []
#bot.gateway.command
def pingpong(resp):
if resp.event.message:
messagelist.append(resp.raw)
bot.gateway.run()
Also, if you're just doing this in the terminal and don't want to reinitialize your gateway every time, you can just clear the commands you've set
bot.gateway.clearCommands()
and clear the current (gateway) session variables
bot.gateway.resetSession()
Discum is intended to be a raw wrapper in order to give the developer maximum freedom. It's also written to be relatively-simple, easy to build-on, and easy-to-use. Hope this helps someone! Happy coding!

Categories