The Prophet ﷺ said, "Allah the Exalted says, 'I am as My servant thinks of Me, and I am with him whenever he remembers Me.'"In another narration, "Allah says, 'I am as My servant thinks of Me, and I am with him whenever he calls upon Me.'"
قال النبي ﷺ: يقول الله تعالى: أنا عند ظن عبدي بي، وأنا معه إذا ذكرني… وفي رواية: إن الله عز وجل يقول: أنا عند ظن عبدي بي، وأنا معه إذا دعاني
Bukhari related it in the Book of Tawhid (7405), and Muslim in the Book of Remembrance and Supplication (2675:2), both through the route of Amash, from Abu Salih, from Abu Hurairah, and he mentioned it with the first wording.As for the other wording, Muslim related it in the Book of Remembrance and Supplication (2675:19) from Abu Kurayb Muhammad bin Ala, who said, Waki narrated to us, from Jafar bin Barqan, from Yazid bin Asam, from Abu Hurairah, and he mentioned it.