Is there a way to get the URL or/and text of a button below the message?
I'm using telethon with python.
You can access the buttons of a message using its message.buttons property.
This property returns a list, each element of list is a row(list) of MessageButton.
For example, if you want to access the URL or text of send music to friends button which is in row 1 and column 0 (0 based index), you can use the following code:
peer_username = "Telegram identifier"
message = client.get_messages(peer_username)[0]
message_button = message.buttons[1][0]
text = message_button.text
url = message_button.url
Sometimes the url property in the MessageButton is empty, and you can use its button property to access the KeyboardButton. For example:
url = message_button.button.url
Related
I'm trying to automatize Outlook with python with win32com.client in python.
I have already a Macro that creates me a email, with all the subject and attached files.
The problem is that when I try to automatize it with Python, i don't know how to select the window that the macro open with all the info, and put the address to whom i want to send.
Example:
I want to send it to "Albert" all the emails that has number "1234" in attach files.
Also, i get error " AttributeError: 'NoneType' object has no attribute 'To' "
outlook = win32.dynamic.Dispatch('Outlook.Application')
namespace = outlook.GetNameSpace('MAPI')
mail = outlook.ActiveWindow().Display()
print(type(mail))
mail. To = "Albert#gmail.com"
mail. Send()
Code from VBA is : (i deleted some details that are not important)
'Generate MailID
strMailID = GenerateMailID
'Generate xls file
strFileName =Environ$("temp") & "/file directory"
'Create mail and attach xls file
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.Subject = "DETAILS FROM EXCEL "
.HTMLBody = "DETAIILS "
.Attachments.Add strFileName
.UserProperties.Add "MailID", 1, False, 1
.UserProperties("MailID") = strMailID
.Display
Firstly, Application.ActiveWindow will return either Explorer or Inspector object, and you only want the latter. The current inspector is returned by Application.ActiveInspector.
And once you have an Inspector object, use Inspector.CurrentItem to retrieve the message being composed.
Thirdly, if you alreay have code that creates the message, why not simply call MailItem.Display to show it to the user? And if the message is shown in an inspector, why do you need to call MailItem.Send instead of letting the user click the Send button when they are ready? You really need to post your code that creates the message.
I am reading .txt file in Python code and I should get the same mail body what I have in my text file.
It is working fine but hyperlinks not displayed in my outlook email, it displays only as text in outlook email.
Below is the code:
Mail_Content = open("MailBody.txt","r")
Read_Content = Mail_Content.read()
In the text file , passing content like this for hyperlink:
linkname,'html'
Please help me out, I am trying to fix this from last two days.
Firstly, you really need to show the code that sets the message body. Secondly, make sure you set the MailItem.HTMLBody rather than the plaintext MailItem.Body.
Make sure the BodyFormat property is set up correctly before setting the HTMLBody property in the code, for example, here is a VBA sample which shows how to set up it properly:
Sub CreateHTMLMail()
'Creates a new email item and modifies its properties.
Dim objMail As MailItem
'Create mail item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY>Type the message text here. </BODY></HTML>"
.Display
End With
End Sub
I should start off mentioning I am pretty new to python and selenium, so I don't really know much about either of them.
what I am trying to do:
Check every second if a telegram channel I follow has sent out a new message. I have telegram web opened on a browser opened with custom flags so that I don't need to log in with telegram every single time.
So far what I did is see the class that has all the message's text inside of it
im_message_text, and get the last of them in the list.
Source Code
NOTE The "strong" tag is simply where the title is stored in side the text message, which is what i'm using to compare everything:
def getItemsInfo1(self):
try:
iteminfo1 = WebDriverWait(self.driver, 4).until(
EC.presence_of_element_located((By.CLASS_NAME, 'im_message_text')))
list3 = self.driver.find_elements_by_class_name("im_message_text")
list33 = list3[-1]
list4 = list33.find_element_by_tag_name("strong")
list4txt = list4.text
return list4txt
except:
print("Nothing is found")
What I do from here is I make another identical code which has a time.sleep(1) at the start to give the message time to appear, and i compare the 2 using the following while loop:
while Main.getItemsInfo1() == Main.getItemsInfo2():
print("No new messages. Try number ", count)
print(Main.getItemsInfo1(), Main.getItemsInfo2())
count += 1
So far, whilst as soon as there is a new message the code output highlights that the 2 methods have different text, it still does not end the while loop and it continues to say that there aren't any new messages.
Does anyone know how to help?
while Main.getItemsInfo1() == Main.getItemsInfo2():
print("No new messages. Try number ", count)
print(Main.getItemsInfo1(), Main.getItemsInfo2())
count += 1
When you call getItemsInfo1() you are calling the method again , so there is no guarantee that the value is get in print statement is same as what is there in while so use something like:
info1= Main.getItemsInfo1()
info2= Main.getItemsInfo2()
while info == Info2:
print("No new messages. Try number ", count)
info1= Main.getItemsInfo1()
info2= Main.getItemsInfo2()
print(info1,info2)
count += 1
The above code will make sure you are having same value, as you are storing the value to a variable and then validating it .
You don't even need to use telegram web or selenuim either, let's make it much more easier:
from telethon import TelegramClient, events
client = TelegramClient(PHONE_NUMBER, API_ID, API_HASH)
#client.on(events.NewMessage("chanel name"))
def new_message_listener(event):
new_message = event.message.message
media_status = event.message.media
## DO what you like with new_messsage
Just remember to change the variables in the TelegramClient, You can receive API_ID, API_HASH from Telegram website.
They should be like this:
api_id = 1027347
api_hash = "c0e2cfefac982659a52da625b81e2a99"
I would like to create an hyperlink in the body of a task created through win32com.
This is my code so far:
outlook = win32com.client.Dispatch("Outlook.Application")
outlook_task_item = 3
recipient = "my_email#site.com"
task = outlook.CreateItem(outlook_task_item)
task.Subject = "hello world"
task.Body = "please update the file here"
task.DueDate = dt.datetime.today()
task.ReminderTime = dt.datetime.today()
task.ReminderSet = True
task.Save()
I have tried to set the property task.HTMLBody but I get the error:
AttributeError: Property 'CreateItem.HTMLBody' can not be set.
I have also tried
task.Body = "Here is the <a href='http://www.python.org'>link</a> I need"
but I am not getting a proper hyperlink.
However if I create a task front end in Outlook, I am able to add hyperlinks.
You can also try:
task.HTMLBody = "Here is the <a href='http://www.python.org'>link</a> I need"
this will overwrite data in 'task.Body' to the HTML format provides in 'task.HTMLBody'
so whichever (Body or HTMLBody) is last will be taken as the Body of the mail.
Tasks do not support HTML. Instead, you have to provide RTF.
You can investigate -- but not set -- the RTF of a given task through task.RTFBody (and task.RTFBody.obj to get a convenient view of it). To use RTF in the body of a task, simply use the task.Body property; setting this to a byte array containing RTF will automatically use that RTF in the body. Concretely, to get the body you want, you could let
task.Body = rb'{\rtf1{Here is the }{\field{\*\fldinst { HYPERLINK "https://www.python.org" }}{\fldrslt {link}}}{ I need}}'
I need your help to order listed item.
I am trying to make apps that can send message to his/her friends ( just like social feeds ). After watching Bret Slatkin talk about create microblogging here's my code:
class Message(ndb.Model):
content = ndb.TextProperty()
created = ndb.DateTimeProperty(auto_now=True)
class MessageIndex(ndb.Model):
receivers = ndb.StringProperty(repeated=True)
class BlogPage(Handler):
def get(self):
if self.request.cookies.get("name"):
user_loggedin = self.request.cookies.get("name")
else:
user_loggedin = None
receive = MessageIndex.query(MessageIndex.receivers == user_loggedin)
receive = receive.fetch()
message_key = [int(r.key.parent().id()) for r in receive]
messages = [Message.get_by_id(int(m)) for m in message_key]
for message in messages:
self.write(message)
The first I do a query to get all message that has my name in the receivers. MessageIndex is child of Message, then I can get key of all message that I receive. And the last is I iter get_by_id using list of message key that I get.
This works fine, but I want to filter each message by its created datetime and thats the problem. The final output is listed item, which cant be ordered using .order or .filter
Maybe some of you can light me up.
You can use the message keys in an 'IN' clause in the Message query. Note that you will need to use the parent() key value, not the id() in this case.
eg:
# dtStart, dtEnd are datetime values
message_keys = [r.key.parent() for r in receive]
query = Message.query(Message._key.IN(message_keys), Message.created>dtStart, Message.created<dtEnd)
query = query.order(Message.created) # or -Message.created for desc
messages = query.fetch()
I am unsure if you wish to simply order by the Message created date, or whether you wish to filter using the date. Both options are catered for above.