AHK Telegram MsgBox (Telegram_MsgBox)

Post your working scripts, libraries and tools
Spur
Posts: 4
Joined: 22 Jul 2014, 02:10

AHK Telegram MsgBox (Telegram_MsgBox)

25 Nov 2016, 04:33

Hi there
I was tired of sending me alerts via mail so I created a function to send me messages via telegram.
Telegram is a messenger like WhatsApp, but it has a API through you can send messages directly to your telegram and so to your phone.
Here is the code with two functions. One is the main function (Telegram_MsgBox) to send messages to telegram the other is just for the first setup. (an easy click-through to get the Bot Token and the Chat ID)

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

RandomVariable = sting with variable: %A_OSType%

Telegram_MsgBox(16,1,"string") 						;icon before text
Telegram_MsgBox(32,2,RandomVariable) 				;icon after text
Telegram_MsgBox("%F0%9F%8E%88",2,RandomVariable) 	;own icon
Telegram_MsgBox(0,a,"string with space")			;no icon	

Telegram_MsgBox(Option,EmojiPosition,Text)
{
	TelegramBotToken = 
	TelegramBotChatID = 
	
	if TelegramBotChatID =
		Telegram_FirstCFG()
	if TelegramBotChatID =
		Telegram_FirstCFG()
	
	if Option=16
		TelegramIconString := "%E2%9D%8C"
	if Option=32
		TelegramIconString := "%E2%9D%94"
	if Option=48
		TelegramIconString := "%E2%9A%A0%EF%B8%8F"
	if Option=64
		TelegramIconString := "%E2%84%B9%EF%B8%8F"
	
	IfInString, Option, `%
		TelegramIconString := Option

	IfLessOrEqual, EmojiPosition, 1
		Text = %TelegramIconString% %Text%
	
	IfGreaterOrEqual, EmojiPosition, 2
		Text = %Text% %TelegramIconString%
		
	loop 3
	{
		UrlDownloadToFile https://api.telegram.org/bot%TelegramBotToken%/sendmessage?chat_id=%TelegramBotChatID%&text=%Text%, %A_ScriptDir%\check.rups
		sleep 1000
		ifexist %A_ScriptDir%\check.rups
		{
			break
		}
		if A_index = 3
		{
			MsgBox, 16,, something went wrong with sending
		}
	}
	filedelete %A_ScriptDir%\check.rups
}

Telegram_FirstCFG()
{
	msgbox open telegram and open a new Chat with @botfater`nif yout dont know how you do that, search in your default serach engine for "Telegram Botfather first bot"`n`nyou need to choose a unique name (you cant change it)
	loop
	{
		InputBox, BotToken, enter the in telegram indicated bot token, 
		MsgBox, 4, , Bot Token correct?:`n%BotToken%
			IfMsgBox Yes
				break
	}
	
	Random, SecurityCode, 1000, 9999
	msgbox now open a new chat with your new created bot (search for @*yourbotname* and start a new chat) and enter this number %SecurityCode% and send it via telegram`n press OK, when done.
	UrlDownloadToFile https://api.telegram.org/bot%BotToken%/getupdates , %A_ScriptDir%\findChatID.txt
	sleep 2000
	run %A_ScriptDir%\findChatID.txt
	msgbox now search for the number %SecurityCode% and on the same line there should be something like: "chat":{"id":782163891`n`nfound it? press ok
	InputBox ChatID, enter the number after "chat":{"id": here:
	FileAppend, Telegram Bot Token = %BotToken%`n, %A_ScriptDir%\TokenAndID.txt
	FileAppend, Telegram Chat ID = %ChatID%, %A_ScriptDir%\TokenAndID.txt
	run %A_ScriptDir%\TokenAndID.txt
	msgbox here you go. copy and past the values to the variables TelegramBotToken & TelegramChatID in the sourcecode.`n`n(you can delte all the .txt files)
	exitapp
}
I orientated the Telegram_MsgBox function on the AHK MsgBox function. But just in point of icons ;)
Telegram_MsgBox(Option, EmojiPosition, Text)
Option
Spoiler
EmojiPositon
Spoiler
Text
Spoiler
Hope you like this funcion and that it will help you. Here are some ideas for what telegram and ahk can be used:
Spoiler
I think telegram has a big potential and maybe sometime in the future we will have a lot of Telegram function. Because Telegram can do sooo much more :)
Sorry for my bad English.
Last edited by Spur on 03 Aug 2018, 01:28, edited 2 times in total.
SpecialG

Re: AHK Telegram MsgBox (Telegram_MsgBox)

25 Nov 2016, 10:30

Nice. Thanks for sharing. I'm testing it with an inputbox but can't send special characters yet.. lol
Feel free to share the image & video part too :beer:
I'm thinking of stuff i could detect locally or monitor on the web and forward to my mobile.
mustang
Posts: 10
Joined: 25 Apr 2017, 03:12

Re: AHK Telegram MsgBox (Telegram_MsgBox)

25 Apr 2017, 03:18

SUPER. Thanks you dear member for this script )))
I needed to post from AHK to telegram my monitoring system.
User avatar
moefr01
Posts: 76
Joined: 25 Nov 2015, 09:01
Location: Germany

Re: AHK Telegram MsgBox (Telegram_MsgBox)

05 May 2017, 09:56

nice code... spur thx :clap:

looking for a solution to send german letters in specified telegram-Messages so I decided to use StringReplace:

Code: Select all

StringReplace, text, text, ä,       `%C3`%A4    , All
StringReplace, text, text, ö,       `%C3`%B6    , All
StringReplace, text, text, ü,       `%C3`%BC    , All
StringReplace, text, text, Ä,       `%C3`%84    , All
StringReplace, text, text, Ö,       `%C3`%96    , All
StringReplace, text, text, Ü,       `%C3`%9C    , All
StringReplace, text, text, Space,   `%20        , All
StringReplace, text, text, ß,       `%C3`%9F    , All
StringReplace, text, text, °,       `%C2`%B0    , All
The variable %text% contents the basic text-string (with german letters) and afterwards the converted code for Telegram ... hope someone could do with it :dance:

happy coding... moefr01
mustang
Posts: 10
Joined: 25 Apr 2017, 03:12

Re: AHK Telegram MsgBox (Telegram_MsgBox)

17 Jun 2017, 06:10

Who now how send other emoji ? example this https://emojipedia.org/man-playing-handball/
how past code U+1F93E ?
User avatar
moefr01
Posts: 76
Joined: 25 Nov 2015, 09:01
Location: Germany

Re: AHK Telegram MsgBox (Telegram_MsgBox)

18 Jun 2017, 01:30

mustang, use this as own ico:

Telegram_MsgBox("%F0%9F%A4%BE",2,RandomVariable)

You can see the working html-code on your browser's statusline by pointing your mouse over the unicode U+1F93E at line Codepoints of your emojipedia.org site.
8-) other Emojis work too... give them a try!
;) moefr01
mustang
Posts: 10
Joined: 25 Apr 2017, 03:12

Re: AHK Telegram MsgBox (Telegram_MsgBox)

18 Jun 2017, 16:47

not worked :( only square. i tryed. work only old emoji

Image
User avatar
moefr01
Posts: 76
Joined: 25 Nov 2015, 09:01
Location: Germany

Re: AHK Telegram MsgBox (Telegram_MsgBox)

19 Jun 2017, 03:49

try it with
FileEncoding, UTF-8
at the beginning of your ahk-code
edwardlobo

Re: AHK Telegram MsgBox (Telegram_MsgBox)

24 Aug 2017, 09:19

If there is "&" in the message or variable the message in truncated
is there any way around it
User avatar
moefr01
Posts: 76
Joined: 25 Nov 2015, 09:01
Location: Germany

Re: AHK Telegram MsgBox (Telegram_MsgBox)

24 Aug 2017, 13:52

Append the following line into the StringReplace-block of my suggestion regarding german Umlauts (see above):

Code: Select all

StringReplace, Infotext, Infotext, &, `%26, All
should work for any ampersand in your string. :wtf:
snowmind
Posts: 123
Joined: 12 Nov 2015, 15:18

Re: AHK Telegram MsgBox (Telegram_MsgBox)

02 Aug 2018, 08:19

Hiiiii
It worked perfectly, but I was left with a doubt. Is it possible to send images?
Spur
Posts: 4
Joined: 22 Jul 2014, 02:10

Re: AHK Telegram MsgBox (Telegram_MsgBox)

03 Aug 2018, 01:11

Hi @all
Thanks for the positive feedback and I am glad to hear one or another could find a use for it.

@snowmind
snowmind wrote:Hiiiii
It worked perfectly, but I was left with a doubt. Is it possible to send images?
Yes there is a way to send photos and videos (and use all other Telegram methods https://core.telegram.org/bots/api#available).
You can do that with cURL.

Code: Select all

Telegram_Token = XXX_Token
Telegram_ChatID = XXX_ChatID
path = %Folder%\%File%
runwait, curl -X POST https://api.telegram.org/%Telegram_Token%/sendPhoto -F chat_id=%Telegram_ChatID% -F photo="@%path%", , Hide
Works for me on Windows 10 1803

Hope that helps.

regards Spur

Edit: Thanks @gregster for pointing out the mistake. :thumbup: I corrected the code.
Last edited by Spur on 05 Aug 2018, 05:35, edited 1 time in total.
gregster
Posts: 3914
Joined: 30 Sep 2013, 06:48

Re: AHK Telegram MsgBox (Telegram_MsgBox)

03 Aug 2018, 07:37

snowmind wrote:Is it possible to send images?
It is also possible without cUrl, but you will have to create multipart/form-data from an image. I can post an example later this weekend.
On Win10 (with cURL already included), cUrl is probably easier to use - but I haven't tried it yet for this purpose.
Last edited by gregster on 03 Aug 2018, 16:55, edited 1 time in total.
gregster
Posts: 3914
Joined: 30 Sep 2013, 06:48

Re: AHK Telegram MsgBox (Telegram_MsgBox)

03 Aug 2018, 07:54

Spur wrote:runwait, curl -X POST https://api.telegram.org/%Telegram_Token%/sendPhoto -F chat_id=%Telegram_ChatID% -F video="@%path%", , Hide
But is this correct? Doesn't have the API command sendPhoto a photo parameter and not a video parameter (which you would find with the command SendVideo?!)
Spur
Posts: 4
Joined: 22 Jul 2014, 02:10

Re: AHK Telegram MsgBox (Telegram_MsgBox)

05 Aug 2018, 05:41

gregster wrote:
Spur wrote:runwait, curl -X POST https://api.telegram.org/%Telegram_Token%/sendPhoto -F chat_id=%Telegram_ChatID% -F video="@%path%", , Hide
But is this correct? Doesn't have the API command sendPhoto a photo parameter and not a video parameter (which you would find with the command SendVideo?!)
thank you for the correction. You are absolutely right. I changed it in the post.
gregster wrote:
snowmind wrote:Is it possible to send images?
It is also possible without cUrl, but you will have to create multipart/form-data from an image. I can post an example later this weekend.
On Win10 (with cURL already included), cUrl is probably easier to use - but I haven't tried it yet for this purpose.
Would love to see an example. Looking forward to see that.
snowmind
Posts: 123
Joined: 12 Nov 2015, 15:18

Re: AHK Telegram MsgBox (Telegram_MsgBox)

05 Aug 2018, 17:50

Thanks "Spur" for sharing this great script. Can you help me with one last question?
I am trying to send messages with "line break" and the "API" does not recognize. Did you get something like that?


CODE:
Words=here is my text`n\and this is a new line`nanother new line
UrlDownloadToFile https://api.telegram.org/bot%TelegramBo ... ext=%Words%, C:\Temp\check.rups
gregster
Posts: 3914
Joined: 30 Sep 2013, 06:48

Re: AHK Telegram MsgBox (Telegram_MsgBox)

05 Aug 2018, 18:10

Hey, snowmind, you can do it like this:

Code: Select all

text := "Let's have a%0Alinebreak."     ; url encoding:  %0A = newline
There are a number of other signs that you need to replace, like 'plus': %2b = plus sign (+)

@spur: it was too hot this weekend 8-) - I will dig it up tomorrow.
snowmind
Posts: 123
Joined: 12 Nov 2015, 15:18

Re: AHK Telegram MsgBox (Telegram_MsgBox)

05 Aug 2018, 18:58

gregster wrote:Hey, snowmind, you can do it like this:

Code: Select all

text := "Let's have a%0Alinebreak."     ; url encoding:  %0A = newline
There are a number of other signs that you need to replace, like 'plus': %2b = plus sign (+)

@spur: it was too hot this weekend 8-) - I will dig it up tomorrow.

Great "gregster" !!
I had been looking for a long time and had not found anything. Thank you very much :dance:
gregster
Posts: 3914
Joined: 30 Sep 2013, 06:48

Re: AHK Telegram MsgBox (Telegram_MsgBox)

05 Aug 2018, 19:03

snowmind wrote:I had been looking for a long time and had not found anything. Thank you very much :dance:
You are welcome :) . You can just look for url encoding or percent encoding: https://en.wikipedia.org/wiki/Percent-encoding.
User avatar
adegard
Posts: 80
Joined: 24 Nov 2017, 05:58
GitHub: adegard

Re: AHK Telegram MsgBox (Telegram_MsgBox)

25 Nov 2018, 15:31

Hi @ Spur great to monitor Scheduled scripts!
It works perfectly...thanks you!

If you'are active again : is it possible to send images ? (I would like to send screenshot from another computer)
Thanks

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Google [Bot], robodesign and 123 guests