@divyacon/piece-telegram-bot 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/LICENSE.MIT-AP +24 -0
  2. package/README.md +8 -2
  3. package/dist/index.d.ts +3 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +122 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/lib/action/answer-callback-query.action.d.ts +8 -0
  8. package/dist/lib/action/answer-callback-query.action.d.ts.map +1 -0
  9. package/dist/lib/action/answer-callback-query.action.js +63 -0
  10. package/dist/lib/action/answer-callback-query.action.js.map +1 -0
  11. package/dist/lib/action/create-invite-link.d.ts +9 -0
  12. package/dist/lib/action/create-invite-link.d.ts.map +1 -0
  13. package/dist/lib/action/create-invite-link.js +66 -0
  14. package/dist/lib/action/create-invite-link.js.map +1 -0
  15. package/dist/lib/action/delete-message.action.d.ts +6 -0
  16. package/dist/lib/action/delete-message.action.d.ts.map +1 -0
  17. package/dist/lib/action/delete-message.action.js +42 -0
  18. package/dist/lib/action/delete-message.action.js.map +1 -0
  19. package/dist/lib/action/edit-message-text.action.d.ts +12 -0
  20. package/dist/lib/action/edit-message-text.action.d.ts.map +1 -0
  21. package/dist/lib/action/edit-message-text.action.js +76 -0
  22. package/dist/lib/action/edit-message-text.action.js.map +1 -0
  23. package/dist/lib/action/forward-message.action.d.ts +10 -0
  24. package/dist/lib/action/forward-message.action.d.ts.map +1 -0
  25. package/dist/lib/action/forward-message.action.js +59 -0
  26. package/dist/lib/action/forward-message.action.js.map +1 -0
  27. package/dist/lib/action/get-chat-member.d.ts +6 -0
  28. package/dist/lib/action/get-chat-member.d.ts.map +1 -0
  29. package/dist/lib/action/get-chat-member.js +49 -0
  30. package/dist/lib/action/get-chat-member.js.map +1 -0
  31. package/dist/lib/action/get-chat.action.d.ts +5 -0
  32. package/dist/lib/action/get-chat.action.d.ts.map +1 -0
  33. package/dist/lib/action/get-chat.action.js +42 -0
  34. package/dist/lib/action/get-chat.action.js.map +1 -0
  35. package/dist/lib/action/get-file.action.d.ts +5 -0
  36. package/dist/lib/action/get-file.action.d.ts.map +1 -0
  37. package/dist/lib/action/get-file.action.js +69 -0
  38. package/dist/lib/action/get-file.action.js.map +1 -0
  39. package/dist/lib/action/pin-message.action.d.ts +7 -0
  40. package/dist/lib/action/pin-message.action.d.ts.map +1 -0
  41. package/dist/lib/action/pin-message.action.js +50 -0
  42. package/dist/lib/action/pin-message.action.js.map +1 -0
  43. package/dist/lib/action/request-approval-message.d.ts +7 -0
  44. package/dist/lib/action/request-approval-message.d.ts.map +1 -0
  45. package/dist/lib/action/request-approval-message.js +84 -0
  46. package/dist/lib/action/request-approval-message.js.map +1 -0
  47. package/dist/lib/action/send-audio.action.d.ts +18 -0
  48. package/dist/lib/action/send-audio.action.d.ts.map +1 -0
  49. package/dist/lib/action/send-audio.action.js +128 -0
  50. package/dist/lib/action/send-audio.action.js.map +1 -0
  51. package/dist/lib/action/send-chat-action.action.d.ts +7 -0
  52. package/dist/lib/action/send-chat-action.action.d.ts.map +1 -0
  53. package/dist/lib/action/send-chat-action.action.js +61 -0
  54. package/dist/lib/action/send-chat-action.action.js.map +1 -0
  55. package/dist/lib/action/send-document.action.d.ts +15 -0
  56. package/dist/lib/action/send-document.action.d.ts.map +1 -0
  57. package/dist/lib/action/send-document.action.js +104 -0
  58. package/dist/lib/action/send-document.action.js.map +1 -0
  59. package/dist/lib/action/send-location.action.d.ts +16 -0
  60. package/dist/lib/action/send-location.action.d.ts.map +1 -0
  61. package/dist/lib/action/send-location.action.js +83 -0
  62. package/dist/lib/action/send-location.action.js.map +1 -0
  63. package/dist/lib/action/send-media-group.action.d.ts +11 -0
  64. package/dist/lib/action/send-media-group.action.d.ts.map +1 -0
  65. package/dist/lib/action/send-media-group.action.js +97 -0
  66. package/dist/lib/action/send-media-group.action.js.map +1 -0
  67. package/dist/lib/action/send-media.action.d.ts +14 -0
  68. package/dist/lib/action/send-media.action.d.ts.map +1 -0
  69. package/dist/lib/action/send-media.action.js +198 -0
  70. package/dist/lib/action/send-media.action.js.map +1 -0
  71. package/dist/lib/action/send-poll.action.d.ts +21 -0
  72. package/dist/lib/action/send-poll.action.d.ts.map +1 -0
  73. package/dist/lib/action/send-poll.action.js +145 -0
  74. package/dist/lib/action/send-poll.action.js.map +1 -0
  75. package/dist/lib/action/send-text-message.action.d.ts +14 -0
  76. package/dist/lib/action/send-text-message.action.d.ts.map +1 -0
  77. package/dist/lib/action/send-text-message.action.js +63 -0
  78. package/dist/lib/action/send-text-message.action.js.map +1 -0
  79. package/dist/lib/action/telegram-answer-callback-query.d.ts +8 -0
  80. package/dist/lib/action/telegram-answer-callback-query.d.ts.map +1 -0
  81. package/dist/lib/action/telegram-answer-callback-query.js +68 -0
  82. package/dist/lib/action/telegram-answer-callback-query.js.map +1 -0
  83. package/dist/lib/action/telegram-create-invite-link.d.ts +8 -0
  84. package/dist/lib/action/telegram-create-invite-link.d.ts.map +1 -0
  85. package/dist/lib/action/telegram-create-invite-link.js +68 -0
  86. package/dist/lib/action/telegram-create-invite-link.js.map +1 -0
  87. package/dist/lib/action/telegram-delete-message.d.ts +5 -0
  88. package/dist/lib/action/telegram-delete-message.d.ts.map +1 -0
  89. package/dist/lib/action/telegram-delete-message.js +44 -0
  90. package/dist/lib/action/telegram-delete-message.js.map +1 -0
  91. package/dist/lib/action/telegram-edit-message-text.d.ts +10 -0
  92. package/dist/lib/action/telegram-edit-message-text.d.ts.map +1 -0
  93. package/dist/lib/action/telegram-edit-message-text.js +93 -0
  94. package/dist/lib/action/telegram-edit-message-text.js.map +1 -0
  95. package/dist/lib/action/telegram-forward-message.d.ts +9 -0
  96. package/dist/lib/action/telegram-forward-message.d.ts.map +1 -0
  97. package/dist/lib/action/telegram-forward-message.js +61 -0
  98. package/dist/lib/action/telegram-forward-message.js.map +1 -0
  99. package/dist/lib/action/telegram-get-bot-info.d.ts +2 -0
  100. package/dist/lib/action/telegram-get-bot-info.d.ts.map +1 -0
  101. package/dist/lib/action/telegram-get-bot-info.js +39 -0
  102. package/dist/lib/action/telegram-get-bot-info.js.map +1 -0
  103. package/dist/lib/action/telegram-get-chat-administrators.d.ts +4 -0
  104. package/dist/lib/action/telegram-get-chat-administrators.d.ts.map +1 -0
  105. package/dist/lib/action/telegram-get-chat-administrators.js +44 -0
  106. package/dist/lib/action/telegram-get-chat-administrators.js.map +1 -0
  107. package/dist/lib/action/telegram-get-chat-member-count.d.ts +4 -0
  108. package/dist/lib/action/telegram-get-chat-member-count.d.ts.map +1 -0
  109. package/dist/lib/action/telegram-get-chat-member-count.js +44 -0
  110. package/dist/lib/action/telegram-get-chat-member-count.js.map +1 -0
  111. package/dist/lib/action/telegram-get-chat-member.d.ts +5 -0
  112. package/dist/lib/action/telegram-get-chat-member.d.ts.map +1 -0
  113. package/dist/lib/action/telegram-get-chat-member.js +51 -0
  114. package/dist/lib/action/telegram-get-chat-member.js.map +1 -0
  115. package/dist/lib/action/telegram-get-chat.d.ts +4 -0
  116. package/dist/lib/action/telegram-get-chat.d.ts.map +1 -0
  117. package/dist/lib/action/telegram-get-chat.js +44 -0
  118. package/dist/lib/action/telegram-get-chat.js.map +1 -0
  119. package/dist/lib/action/telegram-get-file.d.ts +5 -0
  120. package/dist/lib/action/telegram-get-file.d.ts.map +1 -0
  121. package/dist/lib/action/telegram-get-file.js +72 -0
  122. package/dist/lib/action/telegram-get-file.js.map +1 -0
  123. package/dist/lib/action/telegram-pin-message.d.ts +6 -0
  124. package/dist/lib/action/telegram-pin-message.d.ts.map +1 -0
  125. package/dist/lib/action/telegram-pin-message.js +52 -0
  126. package/dist/lib/action/telegram-pin-message.js.map +1 -0
  127. package/dist/lib/action/telegram-send-audio.d.ts +16 -0
  128. package/dist/lib/action/telegram-send-audio.d.ts.map +1 -0
  129. package/dist/lib/action/telegram-send-audio.js +149 -0
  130. package/dist/lib/action/telegram-send-audio.js.map +1 -0
  131. package/dist/lib/action/telegram-send-chat-action.d.ts +6 -0
  132. package/dist/lib/action/telegram-send-chat-action.d.ts.map +1 -0
  133. package/dist/lib/action/telegram-send-chat-action.js +67 -0
  134. package/dist/lib/action/telegram-send-chat-action.js.map +1 -0
  135. package/dist/lib/action/telegram-send-document.d.ts +13 -0
  136. package/dist/lib/action/telegram-send-document.d.ts.map +1 -0
  137. package/dist/lib/action/telegram-send-document.js +125 -0
  138. package/dist/lib/action/telegram-send-document.js.map +1 -0
  139. package/dist/lib/action/telegram-send-location.d.ts +15 -0
  140. package/dist/lib/action/telegram-send-location.d.ts.map +1 -0
  141. package/dist/lib/action/telegram-send-location.js +93 -0
  142. package/dist/lib/action/telegram-send-location.js.map +1 -0
  143. package/dist/lib/action/telegram-send-media-group.d.ts +10 -0
  144. package/dist/lib/action/telegram-send-media-group.d.ts.map +1 -0
  145. package/dist/lib/action/telegram-send-media-group.js +115 -0
  146. package/dist/lib/action/telegram-send-media-group.js.map +1 -0
  147. package/dist/lib/action/telegram-send-message.d.ts +12 -0
  148. package/dist/lib/action/telegram-send-message.d.ts.map +1 -0
  149. package/dist/lib/action/telegram-send-message.js +84 -0
  150. package/dist/lib/action/telegram-send-message.js.map +1 -0
  151. package/dist/lib/action/telegram-send-photo-or-video.d.ts +12 -0
  152. package/dist/lib/action/telegram-send-photo-or-video.d.ts.map +1 -0
  153. package/dist/lib/action/telegram-send-photo-or-video.js +219 -0
  154. package/dist/lib/action/telegram-send-photo-or-video.js.map +1 -0
  155. package/dist/lib/action/telegram-send-poll.d.ts +20 -0
  156. package/dist/lib/action/telegram-send-poll.d.ts.map +1 -0
  157. package/dist/lib/action/telegram-send-poll.js +167 -0
  158. package/dist/lib/action/telegram-send-poll.js.map +1 -0
  159. package/dist/lib/action/telegram-unpin-message.d.ts +5 -0
  160. package/dist/lib/action/telegram-unpin-message.d.ts.map +1 -0
  161. package/dist/lib/action/telegram-unpin-message.js +45 -0
  162. package/dist/lib/action/telegram-unpin-message.js.map +1 -0
  163. package/dist/lib/action/unpin-message.action.d.ts +6 -0
  164. package/dist/lib/action/unpin-message.action.d.ts.map +1 -0
  165. package/dist/lib/action/unpin-message.action.js +43 -0
  166. package/dist/lib/action/unpin-message.action.js.map +1 -0
  167. package/dist/lib/common/index.d.ts +25 -0
  168. package/dist/lib/common/index.d.ts.map +1 -0
  169. package/dist/lib/common/index.js +104 -0
  170. package/dist/lib/common/index.js.map +1 -0
  171. package/dist/lib/output-schemas.d.ts +41 -0
  172. package/dist/lib/output-schemas.d.ts.map +1 -0
  173. package/dist/lib/output-schemas.js +1853 -0
  174. package/dist/lib/output-schemas.js.map +1 -0
  175. package/dist/lib/trigger/new-message.d.ts +15 -0
  176. package/dist/lib/trigger/new-message.d.ts.map +1 -0
  177. package/dist/lib/trigger/new-message.js +121 -0
  178. package/dist/lib/trigger/new-message.js.map +1 -0
  179. package/package.json +44 -3
  180. package/src/index.ts +119 -0
  181. package/src/lib/action/answer-callback-query.action.ts +58 -0
  182. package/src/lib/action/create-invite-link.ts +59 -0
  183. package/src/lib/action/delete-message.action.ts +34 -0
  184. package/src/lib/action/edit-message-text.action.ts +71 -0
  185. package/src/lib/action/forward-message.action.ts +50 -0
  186. package/src/lib/action/get-chat-member.ts +40 -0
  187. package/src/lib/action/get-chat.action.ts +33 -0
  188. package/src/lib/action/get-file.action.ts +79 -0
  189. package/src/lib/action/pin-message.action.ts +41 -0
  190. package/src/lib/action/request-approval-message.ts +85 -0
  191. package/src/lib/action/send-audio.action.ts +123 -0
  192. package/src/lib/action/send-chat-action.action.ts +53 -0
  193. package/src/lib/action/send-document.action.ts +105 -0
  194. package/src/lib/action/send-location.action.ts +76 -0
  195. package/src/lib/action/send-media-group.action.ts +98 -0
  196. package/src/lib/action/send-media.action.ts +209 -0
  197. package/src/lib/action/send-poll.action.ts +140 -0
  198. package/src/lib/action/send-text-message.action.ts +54 -0
  199. package/src/lib/action/telegram-answer-callback-query.ts +64 -0
  200. package/src/lib/action/telegram-create-invite-link.ts +62 -0
  201. package/src/lib/action/telegram-delete-message.ts +37 -0
  202. package/src/lib/action/telegram-edit-message-text.ts +92 -0
  203. package/src/lib/action/telegram-forward-message.ts +54 -0
  204. package/src/lib/action/telegram-get-bot-info.ts +31 -0
  205. package/src/lib/action/telegram-get-chat-administrators.ts +36 -0
  206. package/src/lib/action/telegram-get-chat-member-count.ts +36 -0
  207. package/src/lib/action/telegram-get-chat-member.ts +43 -0
  208. package/src/lib/action/telegram-get-chat.ts +36 -0
  209. package/src/lib/action/telegram-get-file.ts +83 -0
  210. package/src/lib/action/telegram-pin-message.ts +45 -0
  211. package/src/lib/action/telegram-send-audio.ts +157 -0
  212. package/src/lib/action/telegram-send-chat-action.ts +61 -0
  213. package/src/lib/action/telegram-send-document.ts +136 -0
  214. package/src/lib/action/telegram-send-location.ts +89 -0
  215. package/src/lib/action/telegram-send-media-group.ts +119 -0
  216. package/src/lib/action/telegram-send-message.ts +79 -0
  217. package/src/lib/action/telegram-send-photo-or-video.ts +238 -0
  218. package/src/lib/action/telegram-send-poll.ts +170 -0
  219. package/src/lib/action/telegram-unpin-message.ts +38 -0
  220. package/src/lib/action/unpin-message.action.ts +36 -0
  221. package/src/lib/common/index.ts +147 -0
  222. package/src/lib/output-schemas.ts +1909 -0
  223. package/src/lib/trigger/new-message.ts +127 -0
@@ -0,0 +1,238 @@
1
+ import {
2
+ ApFile,
3
+ createAction,
4
+ DynamicPropsValue,
5
+ Property,
6
+ } from '@agnocon/pieces-framework';
7
+ import {
8
+ HttpMessageBody,
9
+ HttpMethod,
10
+ QueryParams,
11
+ httpClient,
12
+ } from '@agnocon/pieces-common';
13
+ import { telegramCommons } from '../common';
14
+ import { telegramBotAuth } from '../..';
15
+ import FormData from 'form-data';
16
+ import { telegramSendPhotoOrVideoActionOutputSchema } from '../output-schemas';
17
+
18
+ export const telegramSendPhotoOrVideo = createAction({
19
+ auth: telegramBotAuth,
20
+ name: 'telegram_send_photo_or_video',
21
+ displayName: 'Send Photo, Video, Sticker or GIF',
22
+ description:
23
+ 'Send a single photo, video, sticker, or animated GIF to a Telegram chat.',
24
+ audience: 'ai',
25
+ aiMetadata: {
26
+ description:
27
+ 'Sends a single photo, video, sticker, or animated GIF to a chat (chat_id is a numeric id or @channelusername the bot can reach), supplied as an uploaded file OR a previously uploaded Telegram file_id, with an optional caption. Pick the media_type, then provide either the file or its file_id for that type. Use for one inline media item; for several in one album use telegram_send_media_group, for an arbitrary attachment use telegram_send_document. Caption format defaults to plain text. Not idempotent: each call posts a new message.',
28
+ idempotent: false,
29
+ },
30
+ outputSchema: telegramSendPhotoOrVideoActionOutputSchema,
31
+ props: {
32
+ chat_id: telegramCommons.chatIdProp(),
33
+ media_type: Property.StaticDropdown({
34
+ displayName: 'Media Type',
35
+ required: true,
36
+ options: {
37
+ disabled: false,
38
+ placeholder: 'Select media type',
39
+ options: [
40
+ { label: 'Image', value: 'photo' },
41
+ { label: 'Video', value: 'video' },
42
+ { label: 'Sticker', value: 'sticker' },
43
+ { label: 'GIF', value: 'animation' },
44
+ ],
45
+ },
46
+ }),
47
+ media: Property.DynamicProperties({
48
+ auth: telegramBotAuth,
49
+ displayName: 'Media Properties',
50
+ required: false,
51
+ refreshers: ['media_type'],
52
+ async props({ media_type }) {
53
+ const propsBuilders: Record<string, () => DynamicPropsValue> = {
54
+ photo: () => ({
55
+ photo: Property.File({
56
+ displayName: 'Image',
57
+ description: 'The image to be uploaded as a file',
58
+ required: false,
59
+ }),
60
+ photoId: Property.ShortText({
61
+ displayName: 'Image Id',
62
+ description:
63
+ "The image file_id previously uploaded to Telegram's servers",
64
+ required: false,
65
+ }),
66
+ }),
67
+ video: () => ({
68
+ video: Property.File({
69
+ displayName: 'Video',
70
+ description: 'The video to be uploaded as a file',
71
+ required: false,
72
+ }),
73
+ videoId: Property.ShortText({
74
+ displayName: 'Video Id',
75
+ description:
76
+ "The video file_id previously uploaded to Telegram's servers",
77
+ required: false,
78
+ }),
79
+ }),
80
+ sticker: () => ({
81
+ sticker: Property.File({
82
+ displayName: 'Sticker',
83
+ description:
84
+ 'The sticker to be uploaded as a file (supports .WEBP files for static and .TGS for animated)',
85
+ required: false,
86
+ }),
87
+ emoji: Property.ShortText({
88
+ displayName: 'Emoji',
89
+ description:
90
+ 'Emoji associated with the sticker. Only for just uploaded stickers',
91
+ required: false,
92
+ }),
93
+ stickerId: Property.ShortText({
94
+ displayName: 'Sticker Id',
95
+ description:
96
+ "The sticker file_id previously uploaded to Telegram's servers",
97
+ required: false,
98
+ }),
99
+ }),
100
+ animation: () => ({
101
+ animation: Property.File({
102
+ displayName: 'GIF',
103
+ description:
104
+ 'The GIF or MPEG-4 without sound file to be uploaded as a auto-playing animation',
105
+ required: false,
106
+ }),
107
+ animationId: Property.ShortText({
108
+ displayName: 'GIF Id',
109
+ description:
110
+ "The GIF or MPEG-4 without sound file_id previously uploaded to Telegram's servers",
111
+ required: false,
112
+ }),
113
+ duration: Property.Number({
114
+ displayName: 'Duration',
115
+ description: 'Duration of sent video in seconds',
116
+ required: false,
117
+ }),
118
+ }),
119
+ };
120
+ const builder = propsBuilders[media_type as unknown as string];
121
+ if (!builder) {
122
+ return {};
123
+ }
124
+ return builder();
125
+ },
126
+ }),
127
+ format: Property.StaticDropdown({
128
+ displayName: 'Caption Format',
129
+ description:
130
+ 'How the caption should be parsed by Telegram. Defaults to plain text; only set MarkdownV2 or HTML when the caption is correctly escaped/tagged.',
131
+ required: false,
132
+ options: {
133
+ options: [
134
+ { label: 'Plain Text', value: 'None' },
135
+ { label: 'Markdown (V2)', value: 'MarkdownV2' },
136
+ { label: 'HTML', value: 'HTML' },
137
+ ],
138
+ },
139
+ defaultValue: 'None',
140
+ }),
141
+ message: Property.LongText({
142
+ displayName: 'Caption',
143
+ description: 'The caption to send with the media',
144
+ required: false,
145
+ }),
146
+ message_thread_id: Property.ShortText({
147
+ displayName: 'Message Thread Id',
148
+ description:
149
+ 'Unique identifier for the target message thread of the forum; supergroup forums only.',
150
+ required: false,
151
+ }),
152
+ disable_notification: telegramCommons.disableNotificationProp(),
153
+ protect_content: telegramCommons.protectContentProp(),
154
+ reply_markup: Property.Json({
155
+ displayName: 'Reply Markup',
156
+ description:
157
+ 'Advanced/optional. A raw JSON-serialized inline keyboard, e.g. {"inline_keyboard":[[{"text":"Open","url":"https://example.com"}]]}. Malformed markup is rejected by Telegram.',
158
+ required: false,
159
+ }),
160
+ },
161
+ async run(ctx) {
162
+ const mediaType = ctx.propsValue['media_type'];
163
+ if (!mediaType) {
164
+ throw new Error('media_type is required');
165
+ }
166
+
167
+ const methods: Partial<Record<string, string>> = {
168
+ photo: 'sendPhoto',
169
+ video: 'sendVideo',
170
+ sticker: 'sendSticker',
171
+ animation: 'sendAnimation',
172
+ };
173
+
174
+ const method = methods[mediaType];
175
+ if (!method) {
176
+ throw new Error('Unknown media type method (' + mediaType + ')');
177
+ }
178
+
179
+ const headers: Record<string, string> = {};
180
+ const queryParams: QueryParams = {};
181
+ let body: HttpMessageBody | undefined = undefined;
182
+
183
+ const file = ctx.propsValue.media?.[mediaType] as ApFile | undefined;
184
+ const id = ctx.propsValue.media?.[mediaType + 'Id'] as string | undefined;
185
+ const parseMode = telegramCommons.resolveParseMode(ctx.propsValue['format']);
186
+
187
+ if (file && file.data && file.filename) {
188
+ const form = new FormData();
189
+ form.append(mediaType, file.data, file.filename);
190
+ form.append('chat_id', ctx.propsValue['chat_id']);
191
+ if (ctx.propsValue['message']) {
192
+ form.append('caption', ctx.propsValue['message']);
193
+ }
194
+ if (ctx.propsValue['message_thread_id']) {
195
+ form.append('message_thread_id', ctx.propsValue['message_thread_id']);
196
+ }
197
+ if (parseMode) {
198
+ form.append('parse_mode', parseMode);
199
+ }
200
+ if (ctx.propsValue['disable_notification']) {
201
+ form.append('disable_notification', 'true');
202
+ }
203
+ if (ctx.propsValue['protect_content']) {
204
+ form.append('protect_content', 'true');
205
+ }
206
+ if (ctx.propsValue['reply_markup']) {
207
+ form.append(
208
+ 'reply_markup',
209
+ JSON.stringify(ctx.propsValue['reply_markup'])
210
+ );
211
+ }
212
+
213
+ body = form;
214
+ Object.assign(headers, form.getHeaders());
215
+ } else if (typeof id !== 'undefined' && id !== '') {
216
+ body = {
217
+ [mediaType]: id,
218
+ chat_id: ctx.propsValue['chat_id'],
219
+ caption: ctx.propsValue['message'] ?? undefined,
220
+ message_thread_id: ctx.propsValue['message_thread_id'] ?? undefined,
221
+ parse_mode: parseMode,
222
+ disable_notification: ctx.propsValue['disable_notification'] ?? false,
223
+ protect_content: ctx.propsValue['protect_content'] ?? false,
224
+ reply_markup: ctx.propsValue['reply_markup'] ?? undefined,
225
+ };
226
+ } else {
227
+ throw new Error('No media defined. Provide either a file or an id.');
228
+ }
229
+
230
+ return await httpClient.sendRequest<never>({
231
+ method: HttpMethod.POST,
232
+ url: telegramCommons.getApiUrl(ctx.auth, method),
233
+ headers,
234
+ body,
235
+ queryParams,
236
+ });
237
+ },
238
+ });
@@ -0,0 +1,170 @@
1
+ import { createAction, Property } from '@agnocon/pieces-framework';
2
+ import { HttpMethod, httpClient } from '@agnocon/pieces-common';
3
+ import { telegramCommons } from '../common';
4
+ import { telegramBotAuth } from '../..';
5
+ import { telegramSendPollActionOutputSchema } from '../output-schemas';
6
+
7
+ export const telegramSendPoll = createAction({
8
+ auth: telegramBotAuth,
9
+ name: 'telegram_send_poll',
10
+ displayName: 'Send Poll',
11
+ description: 'Send a native Telegram poll (regular or quiz) to a chat.',
12
+ audience: 'ai',
13
+ aiMetadata: {
14
+ description:
15
+ 'Posts a native poll (regular or quiz) to a chat addressed by chat_id (a numeric id or @channelusername the bot can reach) with 2–10 answer options. Use to collect votes or run a quiz; quiz polls require correct_option_id (a 0-based index) and cannot allow multiple answers, and open_period and close_date are mutually exclusive. Not idempotent: each call creates a new poll.',
16
+ idempotent: false,
17
+ },
18
+ outputSchema: telegramSendPollActionOutputSchema,
19
+ props: {
20
+ chat_id: telegramCommons.chatIdProp(),
21
+ question: Property.ShortText({
22
+ displayName: 'Question',
23
+ description: 'Poll question, 1–300 characters.',
24
+ required: true,
25
+ }),
26
+ options: Property.Array({
27
+ displayName: 'Options',
28
+ description: 'List of answer options (2–10 options, each 1–100 characters).',
29
+ required: true,
30
+ }),
31
+ is_anonymous: Property.Checkbox({
32
+ displayName: 'Anonymous',
33
+ description: 'True, if the poll needs to be anonymous. Default true.',
34
+ required: false,
35
+ defaultValue: true,
36
+ }),
37
+ type: Property.StaticDropdown({
38
+ displayName: 'Poll Type',
39
+ description: 'Poll type. Defaults to "regular".',
40
+ required: false,
41
+ options: {
42
+ options: [
43
+ { label: 'Regular', value: 'regular' },
44
+ { label: 'Quiz', value: 'quiz' },
45
+ ],
46
+ },
47
+ defaultValue: 'regular',
48
+ }),
49
+ allows_multiple_answers: Property.Checkbox({
50
+ displayName: 'Allow Multiple Answers',
51
+ description: 'True if the poll allows multiple answers (regular polls only).',
52
+ required: false,
53
+ defaultValue: false,
54
+ }),
55
+ correct_option_id: Property.Number({
56
+ displayName: 'Correct Option Id (Quiz)',
57
+ description: '0-based index of the correct answer option (required for quizzes).',
58
+ required: false,
59
+ }),
60
+ explanation: Property.LongText({
61
+ displayName: 'Explanation (Quiz)',
62
+ description: 'Explanation shown when a user picks an incorrect answer (0–200 chars).',
63
+ required: false,
64
+ }),
65
+ explanation_parse_mode: Property.StaticDropdown({
66
+ displayName: 'Explanation Format',
67
+ description:
68
+ 'How the quiz explanation should be parsed by Telegram. Defaults to plain text; only set MarkdownV2 or HTML when the explanation is correctly escaped/tagged.',
69
+ required: false,
70
+ options: {
71
+ options: [
72
+ { label: 'Plain Text', value: 'None' },
73
+ { label: 'Markdown (V2)', value: 'MarkdownV2' },
74
+ { label: 'HTML', value: 'HTML' },
75
+ ],
76
+ },
77
+ defaultValue: 'None',
78
+ }),
79
+ open_period: Property.Number({
80
+ displayName: 'Open Period (seconds)',
81
+ description: 'Amount of time in seconds the poll will be active (5–600).',
82
+ required: false,
83
+ }),
84
+ close_date: Property.DateTime({
85
+ displayName: 'Close Date',
86
+ description: 'Point in time when the poll will be automatically closed.',
87
+ required: false,
88
+ }),
89
+ is_closed: Property.Checkbox({
90
+ displayName: 'Closed',
91
+ description: 'Pass True if the poll should be immediately closed. Useful for previews.',
92
+ required: false,
93
+ defaultValue: false,
94
+ }),
95
+ message_thread_id: Property.ShortText({
96
+ displayName: 'Message Thread Id',
97
+ description:
98
+ 'Unique identifier for the target message thread of the forum; supergroup forums only.',
99
+ required: false,
100
+ }),
101
+ disable_notification: telegramCommons.disableNotificationProp(),
102
+ protect_content: telegramCommons.protectContentProp(),
103
+ reply_to_message_id: telegramCommons.replyToMessageIdProp(),
104
+ reply_markup: Property.Json({
105
+ displayName: 'Reply Markup',
106
+ description:
107
+ 'Advanced/optional. A raw JSON-serialized inline keyboard, e.g. {"inline_keyboard":[[{"text":"Open","url":"https://example.com"}]]}. Malformed markup is rejected by Telegram.',
108
+ required: false,
109
+ }),
110
+ },
111
+ async run(ctx) {
112
+ const options = (ctx.propsValue.options ?? []).map((option) => String(option));
113
+ if (options.length < 2 || options.length > 10) {
114
+ throw new Error('A poll requires between 2 and 10 options.');
115
+ }
116
+ const pollType = ctx.propsValue.type ?? 'regular';
117
+ const correctOptionId = ctx.propsValue.correct_option_id;
118
+ if (pollType === 'quiz') {
119
+ if (correctOptionId === undefined || correctOptionId === null) {
120
+ throw new Error('Quiz polls require "Correct Option Id".');
121
+ }
122
+ if (correctOptionId < 0 || correctOptionId >= options.length) {
123
+ throw new Error(
124
+ `"Correct Option Id" must be between 0 and ${options.length - 1} (the index of an option).`
125
+ );
126
+ }
127
+ if (ctx.propsValue.allows_multiple_answers) {
128
+ throw new Error('Quiz polls cannot allow multiple answers.');
129
+ }
130
+ } else if (correctOptionId !== undefined && correctOptionId !== null) {
131
+ throw new Error(
132
+ '"Correct Option Id" is only valid for quiz polls. Set "Poll Type" to Quiz or clear this field.'
133
+ );
134
+ }
135
+ if (ctx.propsValue.open_period && ctx.propsValue.close_date) {
136
+ throw new Error(
137
+ '"Open Period" and "Close Date" are mutually exclusive — set at most one.'
138
+ );
139
+ }
140
+ const explanationParseMode = telegramCommons.resolveParseMode(
141
+ ctx.propsValue.explanation_parse_mode
142
+ );
143
+
144
+ return await httpClient.sendRequest<never>({
145
+ method: HttpMethod.POST,
146
+ url: telegramCommons.getApiUrl(ctx.auth, 'sendPoll'),
147
+ body: {
148
+ chat_id: ctx.propsValue.chat_id,
149
+ question: ctx.propsValue.question,
150
+ options,
151
+ message_thread_id: ctx.propsValue.message_thread_id ?? undefined,
152
+ is_anonymous: ctx.propsValue.is_anonymous ?? true,
153
+ type: ctx.propsValue.type ?? 'regular',
154
+ allows_multiple_answers: ctx.propsValue.allows_multiple_answers ?? false,
155
+ correct_option_id: ctx.propsValue.correct_option_id ?? undefined,
156
+ explanation: ctx.propsValue.explanation ?? undefined,
157
+ explanation_parse_mode: explanationParseMode,
158
+ open_period: ctx.propsValue.open_period ?? undefined,
159
+ close_date: ctx.propsValue.close_date
160
+ ? Math.floor(new Date(ctx.propsValue.close_date).getTime() / 1000)
161
+ : undefined,
162
+ is_closed: ctx.propsValue.is_closed ?? false,
163
+ disable_notification: ctx.propsValue.disable_notification ?? false,
164
+ protect_content: ctx.propsValue.protect_content ?? false,
165
+ reply_to_message_id: ctx.propsValue.reply_to_message_id ?? undefined,
166
+ reply_markup: ctx.propsValue.reply_markup ?? undefined,
167
+ },
168
+ });
169
+ },
170
+ });
@@ -0,0 +1,38 @@
1
+ import { createAction, Property } from '@agnocon/pieces-framework';
2
+ import { HttpMethod, httpClient } from '@agnocon/pieces-common';
3
+ import { telegramCommons } from '../common';
4
+ import { telegramBotAuth } from '../..';
5
+ import { telegramUnpinMessageActionOutputSchema } from '../output-schemas';
6
+
7
+ export const telegramUnpinMessage = createAction({
8
+ auth: telegramBotAuth,
9
+ name: 'telegram_unpin_message',
10
+ displayName: 'Unpin Message',
11
+ description: 'Unpin a message in a Telegram chat.',
12
+ audience: 'ai',
13
+ aiMetadata: {
14
+ description:
15
+ 'Unpins a message in a chat addressed by chat_id (a numeric id or @channelusername the bot can reach) — supply message_id to unpin a specific message, or omit it to unpin the most recent pinned message; the bot must be an administrator with pin rights. Use to clear a pin set by telegram_pin_message. Idempotent: an already-unpinned message stays unpinned.',
16
+ idempotent: true,
17
+ },
18
+ outputSchema: telegramUnpinMessageActionOutputSchema,
19
+ props: {
20
+ chat_id: telegramCommons.chatIdProp(),
21
+ message_id: Property.ShortText({
22
+ displayName: 'Message Id',
23
+ description:
24
+ 'Identifier of the pinned message to unpin. Leave empty to unpin the most recent pinned message.',
25
+ required: false,
26
+ }),
27
+ },
28
+ async run(ctx) {
29
+ return await httpClient.sendRequest<never>({
30
+ method: HttpMethod.POST,
31
+ url: telegramCommons.getApiUrl(ctx.auth, 'unpinChatMessage'),
32
+ body: {
33
+ chat_id: ctx.propsValue.chat_id,
34
+ message_id: ctx.propsValue.message_id ?? undefined,
35
+ },
36
+ });
37
+ },
38
+ });
@@ -0,0 +1,36 @@
1
+ import { createAction, Property } from '@agnocon/pieces-framework';
2
+ import { HttpMethod, httpClient } from '@agnocon/pieces-common';
3
+ import { telegramCommons } from '../common';
4
+ import { telegramBotAuth } from '../..';
5
+ import { unpinMessageActionOutputSchema } from '../output-schemas';
6
+
7
+ export const telegramUnpinMessageAction = createAction({
8
+ auth: telegramBotAuth,
9
+ name: 'unpin_message',
10
+ displayName: 'Unpin Message',
11
+ description:
12
+ 'Unpin a message in a chat. Leave Message Id empty to unpin the most recent pinned message.',
13
+ audience: 'human',
14
+ aiMetadata: { description: 'Unpins a message in a chat; supply message_id to unpin a specific message, or leave it empty to unpin the most recent pinned message. The bot must be an administrator with pin rights. Idempotent: a message that is already unpinned stays unpinned.', idempotent: true },
15
+ props: {
16
+ instructions: telegramCommons.chatIdInstructions(),
17
+ chat_id: telegramCommons.chatIdProp(),
18
+ message_id: Property.Number({
19
+ displayName: 'Message Id',
20
+ description:
21
+ 'Identifier of the pinned message to unpin. Leave empty to unpin the most recent pinned message.',
22
+ required: false,
23
+ }),
24
+ },
25
+ outputSchema: unpinMessageActionOutputSchema,
26
+ async run(ctx) {
27
+ return await httpClient.sendRequest<never>({
28
+ method: HttpMethod.POST,
29
+ url: telegramCommons.getApiUrl(ctx.auth, 'unpinChatMessage'),
30
+ body: {
31
+ chat_id: ctx.propsValue.chat_id,
32
+ message_id: ctx.propsValue.message_id ?? undefined,
33
+ },
34
+ });
35
+ },
36
+ });
@@ -0,0 +1,147 @@
1
+ import {
2
+ HttpMethod,
3
+ HttpRequest,
4
+ httpClient,
5
+ } from '@agnocon/pieces-common';
6
+ import { telegramBotAuth } from '../..';
7
+ import {
8
+ AppConnectionValueForAuthProperty,
9
+ Property,
10
+ } from '@agnocon/pieces-framework';
11
+
12
+ const chatIdInstructions = `
13
+ **How to obtain Chat ID:**
14
+ 1. Search for the bot "@getmyid_bot" in Telegram.
15
+ 2. Start a conversation with the bot.
16
+ 3. Send the command "/my_id" to the bot.
17
+ 4. The bot will reply with your chat ID.
18
+
19
+ **Note: Remember to initiate the chat with the bot, or you'll get an error for "chat not found."**
20
+ `;
21
+
22
+ const formatLinkMarkdown = `
23
+ [Formatting options](https://core.telegram.org/bots/api#formatting-options)
24
+ `;
25
+
26
+ const buildChatIdInstructions = () =>
27
+ Property.MarkDown({ value: chatIdInstructions });
28
+
29
+ const buildFormatLinkInstructions = () =>
30
+ Property.MarkDown({ value: formatLinkMarkdown });
31
+
32
+ const buildChatIdProp = () =>
33
+ Property.ShortText({
34
+ displayName: 'Chat Id',
35
+ description:
36
+ 'Unique identifier for the target chat or username of the target channel (e.g. @channelusername).',
37
+ required: true,
38
+ });
39
+
40
+ const buildMessageThreadIdProp = () =>
41
+ Property.ShortText({
42
+ displayName: 'Message Thread Id',
43
+ description:
44
+ 'Unique identifier for the target message thread of the forum; supergroup forums only.',
45
+ required: false,
46
+ });
47
+
48
+ const buildParseModeProp = () =>
49
+ Property.StaticDropdown({
50
+ displayName: 'Format',
51
+ description: 'How the message text should be parsed by Telegram.',
52
+ required: false,
53
+ options: {
54
+ options: [
55
+ { label: 'Markdown (V2)', value: 'MarkdownV2' },
56
+ { label: 'HTML', value: 'HTML' },
57
+ { label: 'Plain Text', value: 'None' },
58
+ ],
59
+ },
60
+ defaultValue: 'MarkdownV2',
61
+ });
62
+
63
+ const buildReplyMarkupProp = () =>
64
+ Property.Json({
65
+ required: false,
66
+ displayName: 'Reply Markup',
67
+ description:
68
+ 'Additional interface options. A JSON-serialized object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user.',
69
+ });
70
+
71
+ const buildDisableNotificationProp = () =>
72
+ Property.Checkbox({
73
+ displayName: 'Disable Notification',
74
+ description: 'Send the message silently. Users will receive a notification with no sound.',
75
+ required: false,
76
+ defaultValue: false,
77
+ });
78
+
79
+ const buildProtectContentProp = () =>
80
+ Property.Checkbox({
81
+ displayName: 'Protect Content',
82
+ description: 'Protects the contents of the sent message from forwarding and saving.',
83
+ required: false,
84
+ defaultValue: false,
85
+ });
86
+
87
+ const buildReplyToMessageIdProp = () =>
88
+ Property.Number({
89
+ displayName: 'Reply To Message Id',
90
+ description: 'If the message is a reply, ID of the original message.',
91
+ required: false,
92
+ });
93
+
94
+ const resolveParseMode = (value: string | undefined): string | undefined => {
95
+ if (!value || value === 'None') {
96
+ return undefined;
97
+ }
98
+ return value;
99
+ };
100
+
101
+ export type SetWebhookRequest = {
102
+ ip_address: string;
103
+ max_connections: number;
104
+ allowed_updates: string[];
105
+ drop_pending_updates: boolean;
106
+ secret_token: string;
107
+ };
108
+
109
+ export const telegramCommons = {
110
+ getApiUrl: (auth: AppConnectionValueForAuthProperty<typeof telegramBotAuth>, methodName: string) => {
111
+ return `https://api.telegram.org/bot${auth.secret_text}/${methodName}`;
112
+ },
113
+ subscribeWebhook: async (
114
+ botToken: string,
115
+ webhookUrl: string,
116
+ overrides?: Partial<SetWebhookRequest>
117
+ ) => {
118
+ const request: HttpRequest = {
119
+ method: HttpMethod.POST,
120
+ url: `https://api.telegram.org/bot${botToken}/setWebhook`,
121
+ body: {
122
+ allowed_updates: [],
123
+ url: webhookUrl,
124
+ ...overrides,
125
+ },
126
+ };
127
+
128
+ await httpClient.sendRequest(request);
129
+ },
130
+ unsubscribeWebhook: async (botToken: string) => {
131
+ const request: HttpRequest = {
132
+ method: HttpMethod.GET,
133
+ url: `https://api.telegram.org/bot${botToken}/deleteWebhook`,
134
+ };
135
+ return await httpClient.sendRequest(request);
136
+ },
137
+ chatIdInstructions: buildChatIdInstructions,
138
+ formatLinkInstructions: buildFormatLinkInstructions,
139
+ chatIdProp: buildChatIdProp,
140
+ messageThreadIdProp: buildMessageThreadIdProp,
141
+ parseModeProp: buildParseModeProp,
142
+ replyMarkupProp: buildReplyMarkupProp,
143
+ disableNotificationProp: buildDisableNotificationProp,
144
+ protectContentProp: buildProtectContentProp,
145
+ replyToMessageIdProp: buildReplyToMessageIdProp,
146
+ resolveParseMode,
147
+ };