-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtgprint.m
54 lines (47 loc) · 1.52 KB
/
tgprint.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function tgprint(varargin)
% TGPRINT send an image to a Telegram bot
%
% Use tgprintf() in the same way as sprintf()
% Example: figure(1); plot(x,y);
% tgprint();
%
% There are two sending modes:
% (1) tgprint('photo'): send an image w/ compression using sendPhoto
% (2) tgprint('document'): send an image w/o compression using sendDocument
%
% Define token and chat_id before use,
% which are the authorization token of the target Telegram bot
% and the identifier or username of the target chat
%
% Please refer the following post
% "Creating a Telegram bot for personal notifications"
% https://www.forsomedefinition.com/automation/creating-telegram-bot-notifications/
%
% This also uses urlreadpost by Dan Eills
% https://www.mathworks.com/matlabcentral/fileexchange/27189-urlreadpost-url-post-method-with-binary-file-uploading
%
% Seongsik Park
options = 'photo';
if nargin > 0
options = varargin{1};
end
filename = 'temp.png';
if exist(filename,'file')
warning('temporay file ''%s'' wil be overwritten!',filename);
end
print(gcf,filename,'-dpng');
f = fopen(filename,'rb');
d = fread(f,Inf,'*uint8')';
fclose(f);
% default token and chat_id
token = DEFAULT_TOKEN_HERE
chat_id = DEFAULT_CHAT_ID_HERE;
if strcmpi(options,'photo')
sendstr = ['https://api.telegram.org/bot',token,'/sendPhoto'];
urlreadpost(sendstr,{'chat_id',chat_id,'photo',d});
else
sendstr = ['https://api.telegram.org/bot',token,'/sendDocument'];
urlreadpost(sendstr,{'chat_id',chat_id,'document',d});
end
end