Telegram Automation

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Frosti
Posts: 426
Joined: 27 Oct 2017, 14:30
Contact:

Re: Whatsapp & Telegram Automation

Post by Frosti » 03 Apr 2018, 05:15

Wow that is at least a good approach for me!

Your opinion on the security with the user data I can fully understand. This also keeps me from going on. And there is another problem. My latest programming experience is more than 25 years ago. Perhaps you know, this was a time, there was no object-oriented programming. I gave myself 3 weeks to get an insight into this kind of programming. And the resolve is: "I still do not understand classes and objects". Sometimes I use isolated objects. But I fail at the classes. Thats my problem. I have no time and my brain burns about that.

I started programming last year again out of a necessity. The software I use at work was programmed in 1995. Over the years, more and more features were added. Each on the state of the art. The software is like a vintage car getting constantly new fresh components. You guessed it, it's not the same as a good new car. And this piece of software is specialized, so I can't change it if I don't like it anymore. And the manufacturer of my software has only one thing in mind: "I will be the largest company!". I was trapped. I saw the following: "The computer knows everything about what happen at work, why I can't use that? Why I have spend a lot of time to tell him everytime the same? I had the feeling I'm working for the computer and it doesnt work for me." What a mess, you see? So I start my own project with the big words - like a slogan : "MAXIMUM AUTOMATION!". For that I have more than 30 ideas to change how I and my employee's work in future with computers. Three of thirty idea are nearly ready.

I took money in my hands and had something programmed for me. The program displays the current waiting time in the waiting area (there's running a simple script its showing video's with overlaying data for this day - it works better for me like every "Digital Signage Software" I have tested) and on the Internet. And only for that I would like to continue with Telegram. The users should be able to register via Telegram and will be informed when it's their turn. So no frustration for me anymore with insufferable users and no more frustration for the users. I think that keeps the data leak small! Especially since there will be no direct connection to my data server. The Internet Homepage directs the telegram messages (php) via Internet VPN server to my proxy. This then sends only a few commands to an Autohotkey Script on another computer in another network area and then back. The response time is no problem. I used a very long time for updating (5min) to have not much traffic. The costumers will be informed from the php script.

The problem lies in the age-old database version. They still use DBASE III. To read and not to destroy is a very small way.

Thx, for your help gregster
I wrote this to show others the possibilities. A few months ago I was still thinking about a commercial version.
But Autohotkey is fun for me and I really want to do it with Autohotkey so other people like me can keep control over what's happening on their computer.
We all still loosing it a bit more every day! I wan't to stop it for me and if I can help for others too. My main software doesn't connect any cloud, any internetserver (but that's a lie - I use a GoogleTranslate Function - fuck).
But it's open and easy to read. Breaking their rules is my way!

User avatar
Frosti
Posts: 426
Joined: 27 Oct 2017, 14:30
Contact:

Re: Whatsapp & Telegram Automation

Post by Frosti » 03 Apr 2018, 06:32

I didn‘t answer you right dear gregster. I‘m loosing myself in a monologue. Okay I must use AHK 32 bit on Win7, Win8 and Win10. Because my professionell software runs with 32bit. So I used 32bit as default. I have tried your advices and your script works fine! It‘s not easy to have that already in mind. I‘m sorry for my fault.
The last days I‘m stucked in getting ADO to work for having access to my 40GB DBASE III Database. This was the trick I heard from my companion. But the one is getting very sick and now he leaves his work forever. I don’t like to disturb him. It must be done know without him. I‘am coding about 7 of my ideas right know. If get stuck I go back to the others. Using new knowledge and so on. That’s why I am posting a little bit of something here and there. My brain runs empty at the moment and I can‘t read any code like before. But I feel I have to take all I know and all I had done to make this community richer. Because in the last half year , there was no need for me to ask someone for help in this forum, because I found all I need and much more excellent explained for a beginner like me. And now I‘m still a beginner but a little advanced one.

MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Whatsapp & Telegram Automation

Post by MannyKSoSo » 21 Jul 2018, 17:35

One of the interesting things I have noticed with this program/script is that bots cannot talk to bots. However there is a forward command
forwardMessage
Use this method to forward messages of any kind. On success, the sent Message is returned.
Would it be possible to have a single user with a bunch of bots, have a bot send the user a message, then that message to the targeted bot?
Currently the script by gregster is very interesting, but I only manage to send the Bot the message and instead of it looking like it came from the bot itself and not the user. Also I was looking at a way to view messages sent to those bots (after json of course) so a user can see who sent the message and the message itself.

Any help on the subject is greatly appreciated.

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Whatsapp & Telegram Automation

Post by gregster » 21 Jul 2018, 17:58

Afaik, a Telegram bot can only be used to communicate with users that started (at some point) a communication with this specific bot (to avoid spam). This way, and through the marking as a bot, it is always obvious to a user that he is talking to a bot (which is done on purpose by the Telegram team, afaik).
Of course, if you are talking to your own bots, you could send a message to one bot and get a response from another of your bots. Well, you could do that with other users, too, which are talking to your bots but you can't use a bot to send a message to someone who doesn't know your bot and you can't let a bot pose as a normal user (at least not via the API)
Also I was looking at a way to view messages sent to those bots (after json of course) so a user can see who sent the message and the message itself.
I am not sure, if I understand the question. You can only send messages to your own bots - hence, you can of course - as you control the AHK script - log any message you send yourself via your bots (without using JSON format for the logging), including time, recipient, message content etc... and the receiving user gets the message anyway.

MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Whatsapp & Telegram Automation

Post by MannyKSoSo » 21 Jul 2018, 18:22

To give a better description of what I mean is something like this:
1. User selects who they want to message
2. They write the message and send it to the api program for telegram
3. The message they sent writes to their side (so they can view the history) and the other user gets the message (and the user who sent it) as a side note with this a limited number of history can be shown if the amount of data becomes to large or uses too much resources.

I would like to do this in such a way that I set up each of the users before hand so when I push the code everyone is able to communicate with each other/or only with me if there is an issue with coding. This is why I wanted to use the bots, so that I can set each of the bots up and pull their user name from their computer so each bot is linked with their computer. Hopefully that makes sense.

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Whatsapp & Telegram Automation

Post by gregster » 21 Jul 2018, 18:35

Well, that seems doable.

You could have one bot for personal messages to you. Every user can have a private chat 1-to-1 instance with the bot (that means, to you or rather your script). Either you send automated answers and/or you can get back to certain messages (because you logged them via your script).

And you can have a second bot (could be same like the first one, in theory) that you use in a Telegram group chat to which you invite all known users - there, the bot can talk to all of them and they all can talk to each other (also initiating private chats) and the bot.
Like this, of course every message is visible in every specific chat (private or group) - like in every WhatsApp or Telegram chat I have ever done. Sorry, I don't understand yet your additional logging/history needs. It should all be there, anyway.

Or, do you want to control which of your users can communicate with each other? Well, I guess, that could still be done via the private bot... but do you really need it? The normal Telegram group chats (incl. a bot) and its specific API functions (for banning users etc.) seem like a better alternative.
Last edited by gregster on 21 Jul 2018, 19:10, edited 1 time in total.

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Whatsapp & Telegram Automation

Post by gregster » 21 Jul 2018, 18:57

But I have to admit, I haven't used a bot in a group yet - there are some things to consider, like the "privacy mode" (can be disabled via botfather); and I am not sure atm how to create a bot as a admin of a group (which is optional)... but the API docs clearly indicate that it is possible. I am guessing that you can determine this when you create a group...

MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Whatsapp & Telegram Automation

Post by MannyKSoSo » 21 Jul 2018, 19:16

Since I'm such a noob at autohotkey :P I'll start with the basic data:
First we will start with the main user and the bots, Ill use two bots too keep things simple, but expandable.
Bot 1: {"ok":true,"result":{"id":650086916,"is_bot":true,"first_name":"Test_Bot1","username":"MannyKSoSo_bot"}}
Bot 2: {"ok":true,"result":{"id":509852220,"is_bot":true,"first_name":"Test_Bot2","username":"MannyKSoSo2_bot"}}
GetUpdate Information:

Code: Select all

{"ok":true,"result":[{"update_id":693216748,
"message":{"message_id":1,"from":{"id":602404972,"is_bot":false,"first_name":"MannyKSoSo","username":"MannyKSoSo","language_code":"en-US"},"chat":{"id":602404972,"first_name":"MannyKSoSo","username":"MannyKSoSo","type":"private"},"date":1532210059,"text":"/start","entities":[{"offset":0,"length":6,"type":"bot_command"}]}},{"update_id":693216749,
"message":{"message_id":2,"from":{"id":602404972,"is_bot":false,"first_name":"MannyKSoSo","username":"MannyKSoSo","language_code":"en-US"},"chat":{"id":602404972,"first_name":"MannyKSoSo","username":"MannyKSoSo","type":"private"},"date":1532210068,"text":"/start","entities":[{"offset":0,"length":6,"type":"bot_command"}]}},{"update_id":693216750,
"message":{"message_id":3,"from":{"id":602404972,"is_bot":false,"first_name":"MannyKSoSo","username":"MannyKSoSo","language_code":"en-US"},"chat":{"id":-298181125,"title":"TestingGroup","type":"group","all_members_are_administrators":true},"date":1532210076,"new_chat_participant":{"id":509852220,"is_bot":true,"first_name":"Test_Bot2","username":"MannyKSoSo2_bot"},"new_chat_member":{"id":509852220,"is_bot":true,"first_name":"Test_Bot2","username":"MannyKSoSo2_bot"},"new_chat_members":[{"id":509852220,"is_bot":true,"first_name":"Test_Bot2","username":"MannyKSoSo2_bot"}]}}]}
Next I will link each bot which their respective ID

Code: Select all

Test_Bot1 := 650086916
Test_Bot2 := 509852220
chatID := 602404972
GroupID := -298181125
The next step is to also link each bot with its token (I will post them if needed, but should be similar to what I have above linking each name with their ID)
Then the next step should be with their messages -This is gregsters code

Code: Select all

SendText(token, text, from_id, replyMarkup="",  parseMode="" )
{
		url := "https://api.telegram.org/bot" token "/sendmessage?chat_id=" from_ID "&text=" text "&reply_markup=" replyMarkup "&parse_mode=" parseMode
		json_message := URLDownloadToVar(url)
		return json_message
}
URLDownloadToVar(url,ByRef variable=""){						; function originally by Maestrith, I think
	try																						; keep script from breaking if API is down or not reacting
	{	
		hObject:=ComObjCreate("WinHttp.WinHttpRequest.5.1")
		hObject.Open("GET",url)
		hObject.Send()
		variable:=hObject.ResponseText
		return variable
	}
}
Using the SendText or just doing it via the url it sends the message, however since you cannot send the message to another bot a check should be done to restrict this.
Also, I currently have it set up so the group ID I have that the bots can see the message.
The next step is messages. If I want to have have one bot talk to another in a group chat (without the main user) do I need to setup their own chat group so only they can see it or is there another approach I am not aware of?

Edit: In the main group I have made both bots admins (using the app on my phone)

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Whatsapp & Telegram Automation

Post by gregster » 21 Jul 2018, 19:26

If I want to have have one bot talk to another in a group chat (without the main user) do I need to setup their own chat group so only they can see it or is there another approach I am not aware of?
Why do you want your bots to talk to each other? You control them both... it's not that they will surprise you :)
I am afraid I am still not following.

And no, don't post your Bot tokens. Or other people can control them and use them for spamming etc. in your name.

MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Whatsapp & Telegram Automation

Post by MannyKSoSo » 21 Jul 2018, 19:35

Essentially think of Test_Bot1 as User 1 and Test_Bot2 and User 2. They can use the Group Chat to talk along with the Main User (which is me in this case), but what if User 1 needed to message User 2. Since bots can't talk to each other directly would a second group chat need to be created so they would use the group chat as the conduit or would I need to use the forward command that Telegram has and use my main user as the conduit?

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Whatsapp & Telegram Automation

Post by gregster » 21 Jul 2018, 19:44

But real users in a group (or outside if they know each other) can obviously message each other (and any bot), because they won't be bots. That's what a group is for - so what should this achieve? Telegram Bots (that you control anyway) are obviously not meant for this... this seems like a purely theoretical use case.

MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Whatsapp & Telegram Automation

Post by MannyKSoSo » 21 Jul 2018, 21:24

The idea I had will not work since Telegram does not allow their bots (through url) to read the chat they are in. So even if you send messages to the chat only the user can read them. I can still use this for receiving messages from my users who need help with something using the bots ex.
User 1 reports that there is an error, they can use a send message through a bot which I can receive.
as a side note, if I really wanted a work around I could download the url and extract info that way, but it would be a lot of coding to find the data needed. So technically yes, this is purely theoretical.
But it is entirely possible to download the specific url each time you send a message and receive the data, but extra data would need to be sent since if you download the url it will show all your conversations (in no particular order) so sending a message with a time stamp and looking for a message in the html file with a time stamp greater than what you downloaded from. But again, purely theoretical.

Edit: Again purely theoretical, since bots can't read messages, they can forward them (which receives the text inside) so when a bot sends a message it has a message id, which the script would add 1 to that number so it could find the reply message. The other script looking for that message would continue to loop that message number until it finally was able to forward the message (then receiving the text). With all of this being done by bots in two different groups (1 for the messages and the 2nd for the duplicated messages to extract the text via forwardMessage).

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Whatsapp & Telegram Automation

Post by gregster » 22 Jul 2018, 07:25

I am still thinking that there is some sort of misunderstanding. Downloading urls? Looking for a message in html? You mean from the Telegram client in the browser?
I don't know what this should be used for... you can use 'offset' to keep an overview over the messages. from.id in the JSON response will tell you which user has sent the message (which itself is of course also included in the JSON).

Are you trying to get around the JSON part? That would be a bad idea - because this API, like most APIs, is based on the JSON format. Not using will it make your life harder... and many things impossible.

"Since bots can't read messages" :o - of course, they can read messages from users (my script above reads user messages, parses them for specific "commands" and responds in a certain way - for example, sends messages back) - I don't see what forwarding should do (apart from really forwarding a message). You have the user ids, the time stamps and message IDs all in the JSON responses. So, if you parse them correctly, it will be no problem to look a specific conversations.
The idea I had will not work since Telegram does not allow their bots (through url) to read the chat they are in. So even if you send messages to the chat only the user can read them. I can still use this for receiving messages from my users who need help with something using the bots ex.
The whole idea of bots is that users talk to the bot and you talk (partly automated) - through the bot - back to them. And that is all possible. Look at the JSON updates, there is all information in it that you need. Log all the messages to a text file on your computer - perhaps even one text file per user and you will have the whole chat history with them always at hand.

Of course, you could also create an Autohotkey GUI to display these chats and/or to send messages to a specific user via your bot...

DDook

Re: Whatsapp & Telegram Automation

Post by DDook » 17 Sep 2018, 02:10

gregster
I do not know how to transfer photo files from my computer. How do I transfer photo files from my computer?
Thank you in advance for your help.
(Google translation)

User avatar
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

Re: Whatsapp & Telegram Automation

Post by SL5 » 08 Jan 2019, 01:53

I have to admit I have not read everything.I have already been tried something with telegram and WA.

could you read the username of telegram with your solution?

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Whatsapp & Telegram Automation

Post by gregster » 08 Jan 2019, 02:07

SL5 wrote:
08 Jan 2019, 01:53
could you read the username of telegram with your solution?
Sure, if a Telegram user sends a message to your bot, the bot can also read the unique user id and username of this user.
(Also, first and last name, if available, but many users didn't add this optional information.)

This way it is possible to save settings for specific users and differentiate between users.
Let's say you created a news bot. If some user told your bot he wants only news about 'Sports', you can recognize this user again and send him only sports-related news.

User avatar
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

Re: Whatsapp & Telegram Automation

Post by SL5 » 08 Jan 2019, 02:49

gregster wrote:
08 Jan 2019, 02:07
SL5 wrote:
08 Jan 2019, 01:53
could you read the username ..
Sure ... user id and username ...
can the name also be read out of the active window ( without first sending data of he or me )?

may relevant backlinks: https://www.autohotkey.com/boards/viewt ... 31#p192355

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Whatsapp & Telegram Automation

Post by gregster » 08 Jan 2019, 03:55

I am not sure that we are talking about the same thing here.

Active window of what?
You are linking to a script that uses the official Telegram Bot API for bot-to-user interaction. There are no windows involved (it works "in the background").
And a bot can only communicate with users that initiated the communication with the bot before (who sent a message to the bot before at some point in the past, 5 seconds or 5 weeks, it doesn't matter, if you saved the user information)

If you want to "fake" the interaction of your personal account (not a bot) with other users in your Telegram contact list (user-to-user), this API is not gonna help you. Afaik, this is not officially supported by Telegram.

If you are trying to automate the Telegram desktop app window or the Telegram webapp in a browser, you will have to try using the usual AHK automating techniques for programs or browsers.
I could imagine that you could use IE COM or Chrome.ahk to handle the Telegram browser webapp (or the whatsapp webapp), but I haven't tested it yet (but now that I mention it, I might be interested in actually trying this :ugeek: ). So far, I just did a little testing with clicking and sending to the whatsapp webapp, but that was "in the foreground" and not very sophisticated.

(Btw, the main window of these apps don't necessarily show the Telegram unique username that you can see in the account details of a user. If the user's phone number is in your phone contacts, it will probably show the name that is used there. But that might be what you are actually looking for... instead of the unique Telegram username).

User avatar
SL5
Posts: 879
Joined: 12 May 2015, 02:10
Contact:

Re: Whatsapp & Telegram Automation

Post by SL5 » 08 Jan 2019, 05:29

gregster wrote:
08 Jan 2019, 03:55
... If you are trying to automate the Telegram desktop app window or the Telegram webapp in a browser, you will have to try using the usual AHK automating techniques for programs or browsers. ...
Nevertheless, your bot is very interesting for me :-) :thumbup: :bravo: and I will test it.
There is a solution via bookmarklets that can be combined with a fast mouse to get the username. some of the official browsers seemingly support only one solution via the mouse. Years ago, it was also possible on the keyboard .
if you are interested ... here is an undocumented without instruction: https://github.com/sl5net/global-Intell ... legram.ahk
By the way, with this mouse solution I need a small window and that bothers me sometimes. so i not use it all the time. I'm really not an expert on the graphical use interfaces

gregster
Posts: 8886
Joined: 30 Sep 2013, 06:48

Re: Whatsapp & Telegram Automation

Post by gregster » 11 Jan 2019, 00:59

SL5 wrote:
08 Jan 2019, 05:29
There is a solution via bookmarklets that can be combined with a fast mouse to get the username. some of the official browsers seemingly support only one solution via the mouse. Years ago, it was also possible on the keyboard .
if you are interested ... here is an undocumented without instruction: https://github.com/sl5net/global-Intell ... legram.ahk
Yes, thank you, I will have a look...
So, this is for the Desktop app, right? What info are you extracting there? Username of the last message? And/or the message itself?

Post Reply

Return to “Ask for Help (v1)”