I'm using the Telegram.bot package from nuget and getting the last message as a string from the client. I want then to set it as the attribute of a textbox on a webpage with the control WebBrowser. I'm working on framework 4.7 for Windows Form.
The code I'm using is:
Code:
Private Sub InsertLink(link as string)
MsgBox(link)
WebBrowser1.Document.GetElementById("video_url").SetAttribute("value", link)
WebBrowser1.Document.GetElementById("submitButton").InvokeMember("click")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim botid As String = "my bot api"
Dim bot As New TelegramBotClient(botid)
Dim cts = New CancellationTokenSource()
Dim options = New ReceiverOptions With {
.AllowedUpdates = Array.Empty(Of UpdateType)()
}
bot.StartReceiving(updateHandler:=AddressOf HandleUpdateAsync, errorHandler:=AddressOf HandleErrorAsync, options, cts.Token)
Console.ReadLine()
End Sub
Public link As String
Private Async Function HandleUpdateAsync(botClient As ITelegramBotClient,
update As Update,
cancellationToken As CancellationToken) As Task
Try
If update.Message IsNot Nothing Then
link = update.Message.[text]
InsertLink(link)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Private Function HandleErrorAsync(botClient As ITelegramBotClient,
exception As Exception,
cancellationToken As CancellationToken) As Task
Console.WriteLine(exception)
Return Task.CompletedTask
End Function
End Class
The code, eventhough is not fully async ( since VS shows a message about missing awaits), works. Which means it receives correctly the message from the client as a String.
It also correctly pass the message as a parameter to the Sub InsertLink. But after that, it won't fill the textbox on the webpage.
I want to specify two things:
- I am SURE about the code that Set the Attribute in the webbrowser ( gif under );
- I am SURE about the code that gets the message from telegram client that receive correctly the message as a string type ( gif under ).
Now let's pass to the gifs I recorded. Using breakpoints during debugs, line by line
https://i.stack.imgur.com/Tm0gw.gif
the following gif 2 shows the real functioning of the Set Attribute using a simple button and calling the Sub InsertLink with InsertLink("hello") and the message box ( in italian - Specifiec cast is not valid ) of error when sending a message from the client
https://imgur.com/a/jH0uJuw
And the last gif shows a test I've tried using some window in debug, where I suppose there is a cross thread issue since its written in the debug window Local > Accessibility Object > Message = "Cross Thread, trying to access to form 1 from a different thread".. but seriously I don't know what the issue is, so I just keep scrolling if it can help you to find the issue.
https://i.stack.imgur.com/tsnB4.gif
I'm really struggling with this issue and I'm not able to insert the text from the client to the webbrowser.
the only line on stack trace is :
Code:
Exception thrown: 'System.InvalidCastException' in System.Windows.Forms.dll
and the line Throw New System.Exception inside the catch block of the Private Sub InsertLink fires only
Code:
Exception thrown: 'System.Exception' in YoutubeToMp3.exe