Posting to discord webhook from python - python

REPOST Due To Basically No One Viewing
I am currently working on a python program and im looking for the users computer data to be sent to a discord webhook, the idea is to be that the users Computer's hardware info would be sent to the webhook when they open the program it would also send their public IP address, After i have the program done it will be converted to an exe using pyinstaller so people can open it but they wont be able to edit it. Please see the code i have tried bellow and if anyone can edit/change the code so that it would work and would successfully send the user data over to the webhook that would be a massive help! Thanks
I took the discord webhook text from https://pypi.org/project/discord-webhook/
#getting system info
ip = get('https://api.ipify.org').text
print('Ip address=: {}'.format(ip))
print("="*40, "System Information", "="*40)
uname = platform.uname()
print(f"System: {uname.system}")
print(f"Node Name: {uname.node}")
print(f"Release: {uname.release}")
print(f"Version: {uname.version}")
print(f"Machine: {uname.machine}")
print(f"Processor: {uname.processor}")
#Discord webhook stuff
from discord_webhook import DiscordWebhook, DiscordEmbed
webhook = DiscordWebhook(url="your webhook url", username="New Webhook Username")
embed = DiscordEmbed(
title="Embed Title", description="Your Embed Description", color=242424
)
embed.set_author(
name="Author Name",
url="https://github.com/lovvskillz",
icon_url="https://avatars0.githubusercontent.com/u/14542790",
)
embed.set_footer(text="Embed Footer Text")
embed.set_timestamp()
# Set `inline=False` for the embed field to occupy the whole line
embed.add_embed_field(name="System", value="Lorem ipsum", inline=False)
embed.add_embed_field(name="Name", value="dolor sit", inline=False)
embed.add_embed_field(name="Version", value="amet consetetur")
embed.add_embed_field(name="Processor", value="sadipscing elitr")
webhook.add_embed(embed)
response = webhook.execute()
The system info bit works fine on its own it prints out all the info that you can see, the tricky part is the Discord section as im stuck on how to send the system data over to the discord webhook rather than it just printing out into the console.
Any help would be great
Thanks, Oliver
(I have removed any info such as the discord webhook i was using to stop people from logging into it)

You can just remove (or comment putting a # before) the print line to stop your script from printing the information on the console.
To post the information in your webhook, you can set the values that are in the print statement in the value field of the webhook. Example:
embed.add_embed_field(name="System", value=f"{uname.system}", inline=False)

Related

Get message ID of last sent message from Discord.py webhook

This is the sync code for my discord embed, I've been trying to get the message ID of the message sent so that I can edit it, I'm trying to automate some tasks but been unsuccessful so far.
Here's the code:
embed = discord.Embed(title="Post Title")
embed.add_field(name="Req AMount", value="100")
embed.add_field(name="Return AMount", value="120")
webhook = SyncWebhook.partial(1055498127618097174, "prxoD_ZW0vO6ghrwZNhOiwI9AeLBfNYYss6MTLPkjgvAb_B3WJgWNLMMtPWYZ67GTYbn")
sending = webhook.send(embed=embed)
Any help would be appreciated thanks
In the webhook.send message, if you set the parameter wait to True, you will get a WebhookMessage object returned, which you can then use the edit/delete the message your webhook sent.
Note that the WebhookMessage object doesn't have the ID parameter but has an edit and delete methods. Typically, as webhooks are used to send messages to channels/servers you might not have a bot in/permissions for, what you can do with the returned message object is a little less.
Webhook docs send here
WebhookMessage docs here

Discord Interaction - get relevent data

I'm trying to use discord interaction and I have a few questions.
I successfully sent an interaction post with the following info:
header = { "authorization": ###Auth_Token### }
payload_data = { "type":2,
"application_id":"936929561302675456",
"channel_id":"1031056648250339358",
"session_id":"13cccca154f1265128351c19a71be8e6",
"data": { "version":"994261739745050686",
"id":"938956540159881230",
"name":"imagine",
"type":1,
"options": [ { "type":3,
"name":"prompt",
"value":"Sun"
} ]
}
}
r = requests.post(f"https://discord.com/api/v9/interactions", json=payload_data, headers=header)
Indeed I could see that the message was sent to the server (In this case, the Midjourney Bot) and the bot sent me the wanted information for the payload I sent.
Currently, I manually sent the information that I found in the dev tools (F12) after sending a manual interaction message, and pasted it on the code. The details I'm needing for this stage are:
Auth Token
channel_id
session_id
I wonder if there is any idea to achieve this information without sending any manual interaction message to the server.
With more general information about my intentions, I want to make a script that interacts with the Midjourney Bot via the code and not via the UI itself. I want for example to take user information and send on his behalf a post request to his Midjourney bot and show him the relevant data I get as response.
Currently, I'm working with the requests library of python and not with the discord library but tell me if you have a better solution for accessing this data without generating a new Bot.
Thank you very much

Posting a message in Symphony from Python

I'm trying to send a message from Symphony using Python.
https://developers.symphony.com/restapi/reference#create-message-v4
I found this page but I don't really know how to use it ( There's a cURL and a post url .. ) and I don't understand how I can use requests in this context ( still a beginner in API ).
Can someone help me to figure out how I can use this page to send a message from Python.
Thank you
You have to pass some required things in headers and use multipart/from-data content-type.
If you know about the postman then first with that and pass required headers.
files ={"message":"<messageML>Hello world!</messageML>"}
headers={
"sessionToken": "SESSION_TOKEN",
"keyManagerToken": "KEY_MANAGER_TOKEN"
}
requests.post("https://YOUR-AGENT-URL.symphony.com/agent/v4/stream/:sid/message/create", files=files,headers=headers)
https://github.com/finos/symphony-bdk-python/blob/main/examples has alot of examples on how to use symphony sdk. From python you don't want to use the api. This is the simple code to send a message from a bot. If you do not have a bot set up yet, follow https://docs.developers.symphony.com/building-bots-on-symphony/configuration/configure-your-bot-for-bdk-2.0-for-python NOTE you will need an admin user to follow these steps.
from symphony.bdk.core.config.loader import BdkConfigLoader
from symphony.bdk.core.symphony_bdk import SymphonyBdk
config = BdkConfigLoader.load_from_file("config.yaml")
async with SymphonyBdk(config) as bdk:
streams = bdk.streams()
messages = bdk.messages()
user_id = 123 # this can be found by clicking on a profile and copy profile link eg symphony://?userId=31123123123
stream = await streams.create_im_or_mim([user_id])
await messages.send_message(stream.id, f"<messageML>Message you want to send</messageML>")

How to send bold text using Telegram Python bot

I am writing a telegram bot in Python. I want to send messages with bold letters. I tried to inclose message inside both * and **, but it does not solve the problem.
Is there a function for mark up or HTML formatting or a way to do it?
You should use:
bot.send_message(chat_id=chat_id, text="*bold* Example message",
parse_mode=telegram.ParseMode.MARKDOWN)
Or:
bot.send_message(chat_id=chat_id, text='<b>Example message</b>',
parse_mode=telegram.ParseMode.HTML)
More info at:
https://github.com/python-telegram-bot/python-telegram-bot/wiki/Code-snippets#message-formatting-bold-italic-code-
This is a little bit late. But i hope to be helpful for others:
import telepot
token = 'xxxxxxxxxxxxxxx' # Your telegram token .
receiver_id = yyyyyyyy # Bot id, you can get this by using the next link :
https://api.telegram.org/bot<TOKEN>/getUpdates. Note that you should
replace <TOKEN> with your token.
bot = telepot.Bot(token)
message = "*YOUR MESSAGE YOU WENT TO SEND.*" #Any characters between ** will be
send in bold format.
bot.sendMessage(receiver_id, message , parse_mode= 'Markdown' )

Telegram Bot "chat not found"

I have the following code in Python to send a message to myself from a bot.
import requests
token = '123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHI'
method = 'sendMessage'
myuserid = 1949275XX
response = requests.post(
url='https://api.telegram.org/bot{0}/{1}'.format(token, method),
data={'chat_id': myuserid, 'text': 'hello friend'}
).json()
print(response)
but this returns {'description': 'Bad Request: chat not found', 'error_code': 400, 'ok': False}
What am I doing wrong? I got myuserid by sending /getid to #myidbot and I got my token from #BotFather
As #maak pointed out, you need to first send a message to the bot before the bot can send messages to you.
I was using prefix # before the value of chat_id as suggested everywhere. I removed it and it started working.
Note: if your chat id is 12345678 then you need to prefix it with -100 such that it is -10012345678.
Example Postman call:
/sendMessage?chat_id=-10012345678&text=Let's get together
If your trying to send messages to a group, you must add a ‘-‘ in front of your chat ID.
For example:
TELEGRAM_REG_CHAT_ID="1949275XX"
should be
TELEGRAM_REG_CHAT_ID="-1949275XX"
There is a way to send notifications messages to telegram. It's a bit tricky but the tutorial is great!
http://bernaerts.dyndns.org/linux/75-debian/351-debian-send-telegram-notification
I just sended a message of my apache state to a privat channel.
Works also on public channel but it's not what i wantet. As you call a script (bash) you can prepare the parameters in any script language.
Hope that helps.
For me it worked only with # prefix before channel id
I had some trouble with this after upgrading to a supergroup. The chat_id was updated and it was a bit harder to find this new id.
In the end I solved this with this by following this comment: https://stackoverflow.com/a/56078309/14213187
If you use a username, it does not require any prefix. That means the following are incorrect:
https://t.me/vahid_esmaily_ie
t.me/vahid_esmaily_ie
And this is the correct case:
vahid_esmaily_ie
If you want to use a bot message to the channel, you can refer step here
Steps:
Create a Telegram public channel
Create a Telegram BOT (for example x_bot) via BotFather
Set the x_bot as an administrator in your channel
the chat_id is #x_bot, it's a part of https://t.me/x_bot that does not add your channel name.
Telegram bots can't send messages to user, if that user hasn't started conversation with bot yet, or bot is not present in chat (if it's a group chat). This issue is not related to the library, this is simply Telegram restriction, so that bots can't spam users without their permission.
you need to first send a message to the bot before the bot can send messages to you.

Categories