is there any way to send a reaction to my telegram channel post with python
i checked API on tl.telethon.dev not get any hint about that
thanks in advance
You can use SendReactionRequest in v1.25+:
from telethon.sync import TelegramClient
from telethon import functions, types
result = await client(functions.messages.SendReactionRequest(
peer='username',
msg_id=42,
reaction='❤️'
))
Related
I'm trying to use Telethon to download multiple pinned messages from a group using the following code:
from telethon import TelegramClient, types
async def getPinnedMessages():
async with TelegramClient('MySession', api_id, api_hash) as client:
messages = await client.get_messages('MyGroupChat', ids=types.InputMessagePinned())
The problem is that this returns only a single message, even if there are multiple pinned messages. Any suggestions on what I'm missing here? Thanks.
You need to use InputMessagesFilterPinned:
for message in client.iter_messages(chat, filter=types.InputMessagesFilterPinned()):
... # use message
I am designing an app where I can send notification to my discord channel when something happen with my python code (e.g new user signup on my website). It will be a one way communication as only python app will send message to discord channel.
Here is what I have tried.
import os
import discord
import asyncio
TOKEN = ""
GUILD = ""
def sendMessage(message):
client = discord.Client()
#client.event
async def on_ready():
channel = client.get_channel(706554288985473048)
await channel.send(message)
print("done")
return ""
client.run(TOKEN)
print("can you see me?")
if __name__ == '__main__':
sendMessage("abc")
sendMessage("def")
The issue is only first message is being sent (i-e abc) and then aysn function is blocking the second call (def).
I don't need to listen to discord events and I don't need to keep the network communication open. Is there any way where I can just post the text (post method of api like we use normally) to discord server without listening to events?
Thanks.
You can send the message to a Discord webhook.
First, make a webhook in the Discord channel you'd like to send messages to.
Then, use the discord.Webhook.from_url method to fetch a Webhook object from the URL Discord gave you.
Finally, use the discord.Webhook.send method to send a message using the webhook.
If you're using version 2 of discord.py, you can use this snippet:
from discord import SyncWebhook
webhook = SyncWebhook.from_url("url-here")
webhook.send("Hello World")
Otherwise, you can make use of the requests module:
import requests
from discord import Webhook, RequestsWebhookAdapter
webhook = Webhook.from_url("url-here", adapter=RequestsWebhookAdapter())
webhook.send("Hello World")
I have found it. "Webhook" is the answer. Instead of using discord.py, just create a webhook for your channle and then just post the data to that endpoint.
import requests
#Webhook of my channel. Click on edit channel --> Webhooks --> Creates webhook
mUrl = "https://discord.com/api/webhooks/729017161942******/-CC0BNUXXyrSLF1UxjHMwuHA141wG-FjyOSDq2Lgt*******************"
data = {"content": 'abc'}
response = requests.post(mUrl, json=data)
print(response.status_code)
print(response.content)
This might be one of the best approaches as it saves the addition of more python packages(one mentioned by #john), but I believe there is a more robust and easy solution for this scenario, as you can add images, make tables and make those notifications look more expressive.
A python library designed for the explicit purpose of sending a message to the discord server. A simple example from the PyPI page would be:
from discord_webhook import DiscordWebhook
webhook = DiscordWebhook(url='your webhook url', content='Webhook Message')
response = webhook.execute()
more examples follow on the page.
This is how the sent notification/message would look like
Discord notification with table
I want to send a private message to a specific user with my discord bot. I am using discord==1.0.1
discord.py==1.3.1. I already tried using the docs (https://discordpy.readthedocs.io/en/latest/api.html?highlight=private%20message#discord.DMChannel) but i dont understand that. I tried following code, which didnt worked:
#client.command()
async def cmds(ctx):
embed = discord.Embed(title='Discord Server Befehle',
description='Description',
color=0x374883)
[...]
msg = await ctx.send(embed=embed)
await ctx.author.create_dm('Hi')
print(f'{command_prefix}help ausgeführt von {ctx.author}')
Try using the send function instead of the create_dm. create_dm only creates the channel between two users which discord does automatically as far as i know.
according to documentation
This should be rarely called, as this is done transparently for most people.
so it should be
ctx.author.send('Hi')
When I get the history chat message with a bot I can't see the "hello" message ("What can this bot do?") in the return message_context. How can I get it?
message_context = client.send_message(bot_name, '/start')
for message in client.iter_messages(bot_name):
print("{}".format(bot_name))
You need to use GetFullUserRequest to fetch the bot_info attribute of the UserFull instance:
from telethon.sync import TelegramClient
from telethon import functions, types
with TelegramClient(name, api_id, api_hash) as client:
result = client(functions.users.GetFullUserRequest(bot_name))
bot_info = result.bot_info
print(bot_info)
print(bot_info.description)
I am into one telegram group. I wanted to read those messages in my python code. Is there any way to read those messages without adding bot in that group.. for example abc is my user id... And abc is added in xyz group. So wanted to read xyz group message in my python code.
Yes, you can do that via using Telegram API named Telethon.
Telethon Github
Here is an example for setting up processes of the Telethon API. I've written this code to pull all newly posted images from one telegram group. It will give you an idea of how to start to use it.
import sys
import os
from telethon import TelegramClient
from telethon.tl.functions.messages import GetFullChatRequest
from telethon.tl.functions.messages import GetHistoryRequest
from telethon.tl.functions.channels import GetChannelsRequest
from telethon.tl.functions.contacts import ResolveUsernameRequest
from telethon.tl.types import PeerUser, PeerChat, PeerChannel
import re
# These example values won't work. You must get your own api_id and
# api_hash from https://my.telegram.org, under API Development.
api_id = 11111 #number
api_hash = 'x'#string
phone = 'x'
client = TelegramClient('session_name', api_id, api_hash,update_workers=1, spawn_read_thread=False)
client.connect()
Also if you are interested in all of my code on this Telethon integration, you can find it in the following GitHub link;
Telegram Group Bot
Based on this thread : thread
I found this code really works for me. Note that this is a synchronous function, if you want to use multiple channels at the same time you may need to make use of asyncio. Detailed information about that in the next link --> Async Quick-Start
from telethon import TelegramClient, events, sync
api_id = ...
api_hash = '...'
client = TelegramClient('anon', api_id, api_hash)
#client.on(events.NewMessage(chats='channel_name'))
async def my_event_handler(event):
print(event.raw_text)
client.start()
client.run_until_disconnected()
For the code above, you have to replace "channel_name" with the name of the group your user is on.
api_id and api_hash are obtained when you setup your API app in Telegram.